@@ -33,7 +33,7 @@ s 只包含小写英文字母。
3333
3434```
3535
36- ## 暴力法(AC)
36+ ## 暴力法 + 剪枝
3737
3838### 思路
3939
@@ -71,7 +71,7 @@ class Solution:
7171- 时间复杂度:双层循环找出所有子串的复杂度是$O(n^2)$,统计元音个数复杂度也是$O(n)$,因此这种算法的时间复杂度为$O(n^3)$。
7272- 空间复杂度:$O(1)$
7373
74- ## 前缀和(TLE)
74+ ## 前缀和 + 剪枝
7575
7676### 思路
7777
@@ -104,7 +104,6 @@ class Solution:
104104 return True
105105 def findTheLongestSubstring (self , s : str ) -> int :
106106 n = len (s)
107- res = 0
108107
109108 pre = [[0 ] * 5 for _ in range (n)]
110109
@@ -115,11 +114,11 @@ class Solution:
115114 pre[i][j] = pre[i - 1 ][j] + 1
116115 else :
117116 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
123122```
124123
125124Java Code:
@@ -152,19 +151,14 @@ class Solution {
152151 }
153152 }
154153
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-- ) {
159155
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
164159 }
165160 }
166-
167- return maxLen;
161+ return 0
168162 }
169163
170164
@@ -201,7 +195,7 @@ class Solution {
201195- 时间复杂度:$O(n^2)$。
202196- 空间复杂度:$O(n)$
203197
204- ## 前缀和 + 状态压缩(AC)
198+ ## 前缀和 + 状态压缩 c
205199
206200### 思路
207201
0 commit comments