为leetcode392题增加后续挑战题解

This commit is contained in:
hanggegreat 2019-06-29 12:29:40 +08:00
parent a2220c04de
commit 03a8cd27e4

View File

@ -269,6 +269,48 @@ public boolean isSubsequence(String s, String t) {
}
```
后续挑战 :
如果有大量输入的 S称作S1, S2, ... , Sk 其中 k >= 10亿你需要依次检查它们是否为 T 的子序列在这种情况下你会怎样改变代码
对于这个后续挑战因为有10亿以上的S所以不能每次都去遍历T而是应该把T的序列保存下来
定义一个dp数组int [][]dp = new int[t.length()][26]其中dp[i][j] 表示T的i位置及之后(j + 'a')对应的字符最早出现的位置
之后对S进行遍历用tIndex记录T所到达的位置并判断T的tIndex位置及之后是否有S当前位置元素遍历完成返回true如果在这期间tIndex越界则返回false
```java
public boolean isSubsequence(String s, String t) {
int [][]dp = new int[t.length()][26];
int []pos = new int[26];
for (int i = 0; i < 26; i++) {
pos[i] = t.length();
}
for (int i = dp.length - 1; i >= 0; i--) {
pos[t.charAt(i) - 'a'] = i;
for (int j = 0; j < 26; j++) {
dp[i][j] = pos[j];
}
}
int tIndex = 0;
for (int i = 0; i < s.length(); i++) {
if (tIndex >= t.length()) {
return false;
}
tIndex = dp[tIndex][s.charAt(i) - 'a'];
if (tIndex >= t.length()) {
return false;
}
tIndex++;
}
return true;
}
```
# 9. 修改一个数成为非递减数组
[665. Non-decreasing Array (Easy)](https://leetcode.com/problems/non-decreasing-array/description/)