#Leetcode小白上线第三十二天#
1.JAVA 判断子序列
题目
给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=100)。
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。
示例 1:
s = “abc”, t = “ahbgdc”
返回 true.
示例 2:
s = “axc”, t = “ahbgdc”
返回 false.
后续挑战 :
如果有大量输入的 S,称作S1, S2, … , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码?
致谢:
特别感谢 @pbrother 添加此问题并且创建所有测试用例。
通过次数71,339提交次数142,087
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/is-subsequence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
题解
// class Solution {
// public boolean isSubsequence(String s, String t) {
// int sum=0,k=-1;
// for(int i=0;i<s.length();i++){
// for(int j=0;j<t.length();j++){
// if(s.charAt(i)t.charAt(j)&&k!=j&&k<=j)
// {
// k=j ;
// sum++ ;
// }
// }
// }
// if(sums.length())
// return true;
// return false;
// }
// }
class Solution {
public boolean isSubsequence(String s, String t) {
char[] S = s.toCharArray();
char[] T = t.toCharArray();
int[] temp = new int[s.length()];
int count = 0;
if(s.equals(t)) return true;
if(!t.isEmpty() && s.length() <= t.length()){
for(int i = 0; i < charS.length; i++){
temp[i] = -1;
for(int j = count; j < charT.length; j++){
if(S[i] == T[j]){
temp[i] = j;
count = ++j;
break;
}else {
count = j;
}
}
}
for(int i = 0; i < temp.length; i++){
if(temp[i] == -1){
return false;
}
}
return true;
}
return false;
}
}
总结
口语,明天开始准备英语阅读,FCC前段学习的复习,算法学习,加油呀!