File tree Expand file tree Collapse file tree 2 files changed +34
-34
lines changed
solution/src/main/java/com/inuker/solution
test/src/main/java/com/inuker/test Expand file tree Collapse file tree 2 files changed +34
-34
lines changed Original file line number Diff line number Diff line change @@ -8,43 +8,30 @@ public class MinimumWindowSubstring {
8
8
9
9
// 耗时8ms,时间复杂度O(n)
10
10
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 ];
13
12
for (char c : t .toCharArray ()) {
14
13
tc [c ]++;
15
14
}
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 ++) {
20
17
char c = s .charAt (j );
21
-
22
18
if (++sc [c ] <= tc [c ]) {
23
- ++k ;
19
+ ++count ;
24
20
}
25
-
26
- if (k == t .length ()) {
27
- for (; i < j ; i ++) {
21
+ if (count == t .length ()) {
22
+ for ( ; i < j ; i ++) {
28
23
char cc = s .charAt (i );
29
-
30
- if (tc [cc ] == 0 ) {
31
- continue ;
32
- }
33
-
34
24
if (sc [cc ] <= tc [cc ]) {
35
25
break ;
36
26
}
37
-
38
27
sc [cc ]--;
39
28
}
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 ;
44
32
}
45
33
}
46
34
}
47
-
48
- return minLen != Integer .MAX_VALUE ? s .substring (minStart , minStart + minLen ) : "" ;
35
+ return min == Integer .MAX_VALUE ? "" : s .substring (start , start + min );
49
36
}
50
37
}
Original file line number Diff line number Diff line change 28
28
public class main {
29
29
30
30
public static void main (String [] args ) {
31
- System .out .println (isPalindrome ( 10 ));
31
+ System .out .println (minWindow ( "ADOBECODEBANC" , "ABC" ));
32
32
}
33
33
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 ]++;
37
38
}
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
+ }
44
57
}
45
58
}
46
- return true ;
59
+ return min == Integer . MAX_VALUE ? "" : s . substring ( start , start + min ) ;
47
60
}
48
61
}
You can’t perform that action at this time.
0 commit comments