@@ -33,7 +33,7 @@ s 只包含小写英文字母。
33
33
34
34
```
35
35
36
- ## 暴力法(AC)
36
+ ## 暴力法 + 剪枝
37
37
38
38
### 思路
39
39
@@ -71,7 +71,7 @@ class Solution:
71
71
- 时间复杂度:双层循环找出所有子串的复杂度是$O(n^2)$,统计元音个数复杂度也是$O(n)$,因此这种算法的时间复杂度为$O(n^3)$。
72
72
- 空间复杂度:$O(1)$
73
73
74
- ## 前缀和(TLE)
74
+ ## 前缀和 + 剪枝
75
75
76
76
### 思路
77
77
@@ -104,7 +104,6 @@ class Solution:
104
104
return True
105
105
def findTheLongestSubstring (self , s : str ) -> int :
106
106
n = len (s)
107
- res = 0
108
107
109
108
pre = [[0 ] * 5 for _ in range (n)]
110
109
@@ -115,11 +114,11 @@ class Solution:
115
114
pre[i][j] = pre[i - 1 ][j] + 1
116
115
else :
117
116
pre[i][j] = pre[i - 1 ][j]
118
- for i in range (n):
119
- for j in range (i, n ):
120
- if self .check(s, pre, i, j):
121
- res = max (res, j - i + 1 )
122
- return res
117
+ for i in range (n - 1 , - 1 , - 1 ):
118
+ for j in range (n - i ):
119
+ if self .check(s, pre, j, i + j):
120
+ return i + 1
121
+ return 0
123
122
```
124
123
125
124
Java Code:
@@ -152,19 +151,14 @@ class Solution {
152
151
}
153
152
}
154
153
155
- // find max
156
- int maxLen = 0 ;
157
-
158
- for (int i = 0 ; i < len; i++ ) {
154
+ for (int i = len - 1 ; i >= 0 ; i-- ) {
159
155
160
- for (int j = i; j < len; j++ ) {
161
-
162
- if (checkValid(preSum, s, i, j))
163
- maxLen = Math . max(maxLen, j - i + 1 );
156
+ for (int j = 0 ; j < len - i; j++ ) {
157
+ if (checkValid(preSum, s, i, i + j))
158
+ return i + 1
164
159
}
165
160
}
166
-
167
- return maxLen;
161
+ return 0
168
162
}
169
163
170
164
@@ -201,7 +195,7 @@ class Solution {
201
195
- 时间复杂度:$O(n^2)$。
202
196
- 空间复杂度:$O(n)$
203
197
204
- ## 前缀和 + 状态压缩(AC)
198
+ ## 前缀和 + 状态压缩 c
205
199
206
200
### 思路
207
201
0 commit comments