public int[] shortestSeq(int[] big, int[] small) {
int bigLen = big.length;
int smallLen = small.length;
int start = 0;
int end = 0;
int[] result = new int[0];
int minLen = bigLen;
Map<Integer, Integer> count = new HashMap<>();
for (int i : small) {
count.put(i, 1);
}
while (end < bigLen) {
if (count.containsKey(big[end])) {
if (count.get(big[end]) > 0) {
smallLen --;
}
count.put(big[end], count.get(big[end]) - 1);
}
while (smallLen == 0) {
if (minLen > end - start) {
minLen = end - start;
result = new int[]{start, end};
}
if (count.containsKey(big[start])) {
count.put(big[start], count.get(big[start]) + 1);
if (count.get(big[start]) > 0) {
smallLen ++;
}
}
start ++;
}
end ++;
}
return result;
}
滑动窗口解决字符串查找问题
最新推荐文章于 2024-07-25 11:44:32 发布