使用队列要简单一些 | #判断子序列#
判断子序列
https://www.nowcoder.com/practice/39be6c2d0a9b4c30a7b04053d5960a84
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param S string字符串
* @param T string字符串
* @return bool布尔型
*/
public boolean isSubsequence (String S, String T) {
// write code here
if (S.equals(T) || T.contains(S)) return true;
LinkedList<Character> linkedList = new LinkedList<>();
for (int i = 0; i < S.length(); i++) {
linkedList.offer(S.charAt(i));
}
for (int i = 0; i < T.length(); i++) {
if (linkedList.isEmpty()) return true;
if (T.charAt(i) == linkedList.peek()) {
linkedList.poll();
}
}
return linkedList.isEmpty();
}
}
