Skip to content

Commit c9f9ff5

Browse files
committed
fd
1 parent e74e787 commit c9f9ff5

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

solution/src/main/java/com/inuker/solution/MinimumWindowSubstring.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,30 @@ public class MinimumWindowSubstring {
88

99
// 耗时8ms,时间复杂度O(n)
1010
public String minWindow(String s, String t) {
11-
int[] sc = new int[256], tc = new int[256];
12-
11+
int[] tc = new int[256], sc = new int[256];
1312
for (char c : t.toCharArray()) {
1413
tc[c]++;
1514
}
16-
17-
int minStart = 0, minLen = Integer.MAX_VALUE;
18-
19-
for (int i = 0, j = 0, k = 0; j < s.length(); j++) {
15+
int count = 0, min = Integer.MAX_VALUE, start = 0;
16+
for (int i = 0, j = 0; j < s.length(); j++) {
2017
char c = s.charAt(j);
21-
2218
if (++sc[c] <= tc[c]) {
23-
++k;
19+
++count;
2420
}
25-
26-
if (k == t.length()) {
27-
for (; i < j; i++) {
21+
if (count == t.length()) {
22+
for ( ; i < j; i++) {
2823
char cc = s.charAt(i);
29-
30-
if (tc[cc] == 0) {
31-
continue;
32-
}
33-
3424
if (sc[cc] <= tc[cc]) {
3525
break;
3626
}
37-
3827
sc[cc]--;
3928
}
40-
41-
if (j - i + 1 < minLen) {
42-
minLen = j - i + 1;
43-
minStart = i;
29+
if (j - i + 1 < min) {
30+
min = j - i + 1;
31+
start = i;
4432
}
4533
}
4634
}
47-
48-
return minLen != Integer.MAX_VALUE ? s.substring(minStart, minStart + minLen) : "";
35+
return min == Integer.MAX_VALUE ? "" : s.substring(start, start + min);
4936
}
5037
}

test/src/main/java/com/inuker/test/main.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,34 @@
2828
public class main {
2929

3030
public static void main(String[] args) {
31-
System.out.println(isPalindrome(10));
31+
System.out.println(minWindow("ADOBECODEBANC", "ABC"));
3232
}
3333

34-
public static boolean isPalindrome(int x) {
35-
if (x < 0) {
36-
return false;
34+
public static String minWindow(String s, String t) {
35+
int[] tc = new int[256], sc = new int[256];
36+
for (char c : t.toCharArray()) {
37+
tc[c]++;
3738
}
38-
int t = 1;
39-
for (; t <= x / 10; t *= 10);
40-
41-
for (int k = 1; t > k; t /= 10, k *= 10) {
42-
if ((x / t) % 10 != (x / k) % 10) {
43-
return false;
39+
int count = 0, min = Integer.MAX_VALUE, start = 0;
40+
for (int i = 0, j = 0; j < s.length(); j++) {
41+
char c = s.charAt(j);
42+
if (++sc[c] <= tc[c]) {
43+
++count;
44+
}
45+
if (count == t.length()) {
46+
for ( ; i < j; i++) {
47+
char cc = s.charAt(i);
48+
if (sc[cc] <= tc[cc]) {
49+
break;
50+
}
51+
sc[cc]--;
52+
}
53+
if (j - i + 1 < min) {
54+
min = j - i + 1;
55+
start = i;
56+
}
4457
}
4558
}
46-
return true;
59+
return min == Integer.MAX_VALUE ? "" : s.substring(start, start + min);
4760
}
4861
}

0 commit comments

Comments
 (0)