-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution76.java
More file actions
executable file
·32 lines (32 loc) · 1.1 KB
/
Copy pathSolution76.java
File metadata and controls
executable file
·32 lines (32 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution76 {
public String minWindow(String s, String t) {
int[] counts = new int[128];
int count = 0, len = Integer.MAX_VALUE;
String res = "";
// 计数 t 字符串
for (int i = 0; i < t.length(); i++) {
int index = t.charAt(i);
counts[index]--;
if (counts[index] == -1) count++;
}
// 滑动窗口
int left = 0;
for (int right = 0; right < s.length(); right++) { // 窗口右移
int index = s.charAt(right);
if (count > 0) {
if (++counts[index] == 0) count--;
}
if (count == 0) { // 窗口左移,同时保持右边界不变
if (right - left + 1 < len) {
len = right - left + 1;
res = s.substring(left, right + 1);
}
index = s.charAt(left);
if (--counts[index] < 0) count++; // 表示某个需要匹配的字符缺少了
left++;
if (count == 0) right--;
}
}
return res;
}
}