@@ -75,7 +75,7 @@ for (int i = 0, j = 0; i < n; ++i) {
75
75
class Solution :
76
76
def lengthOfLongestSubstring (self , s : str ) -> int :
77
77
ss = set ()
78
- i = ans = 0
78
+ ans = i = 0
79
79
for j, c in enumerate (s):
80
80
while c in ss:
81
81
ss.remove(s[i])
@@ -88,14 +88,14 @@ class Solution:
88
88
``` java
89
89
class Solution {
90
90
public int lengthOfLongestSubstring (String s ) {
91
- Set< Character > ss = new HashSet<> () ;
92
- int i = 0 , ans = 0 ;
93
- for (int j = 0 ; j < s. length(); ++ j) {
91
+ boolean [] ss = new boolean [ 128 ] ;
92
+ int ans = 0 ;
93
+ for (int i = 0 , j = 0 ; j < s. length(); ++ j) {
94
94
char c = s. charAt(j);
95
- while (ss. contains(c) ) {
96
- ss. remove( s. charAt(i++ )) ;
95
+ while (ss[c] ) {
96
+ ss[ s. charAt(i++ )] = false ;
97
97
}
98
- ss. add(c) ;
98
+ ss[c] = true ;
99
99
ans = Math . max(ans, j - i + 1 );
100
100
}
101
101
return ans;
@@ -107,11 +107,13 @@ class Solution {
107
107
class Solution {
108
108
public:
109
109
int lengthOfLongestSubstring(string s) {
110
- unordered_set<char > ss;
111
- int i = 0, ans = 0;
112
- for (int j = 0; j < s.size(); ++j) {
113
- while (ss.count(s[ j] )) ss.erase(s[ i++] );
114
- ss.insert(s[ j] );
110
+ bool ss[ 128] {};
111
+ int ans = 0;
112
+ for (int i = 0, j = 0; j < s.size(); ++j) {
113
+ while (ss[ s[ j]] ) {
114
+ ss[ s[ i++]] = false;
115
+ }
116
+ ss[ s[ j]] = true;
115
117
ans = max(ans, j - i + 1);
116
118
}
117
119
return ans;
@@ -120,31 +122,30 @@ public:
120
122
```
121
123
122
124
```go
123
- func lengthOfLongestSubstring(s string) int {
124
- ss := map[byte]bool{}
125
- i, ans := 0, 0
126
- for j := 0; j < len(s); j++ {
125
+ func lengthOfLongestSubstring(s string) (ans int) {
126
+ ss := [128]bool{}
127
+ for i, j := 0, 0; j < len(s); j++ {
127
128
for ss[s[j]] {
128
129
ss[s[i]] = false
129
130
i++
130
131
}
131
132
ss[s[j]] = true
132
133
ans = max(ans, j-i+1)
133
134
}
134
- return ans
135
+ return
135
136
}
136
137
```
137
138
138
139
``` ts
139
140
function lengthOfLongestSubstring(s : string ): number {
140
141
let ans = 0 ;
141
- const vis = new Set < string > ();
142
- for (let i = 0 , j = 0 ; i < s .length ; ++ i ) {
143
- while (vis .has (s [i ])) {
144
- vis .delete (s [j ++ ]);
142
+ const ss : Set < string > = new Set ();
143
+ for (let i = 0 , j = 0 ; j < s .length ; ++ j ) {
144
+ while (ss .has (s [j ])) {
145
+ ss .delete (s [i ++ ]);
145
146
}
146
- vis .add (s [i ]);
147
- ans = Math .max (ans , i - j + 1 );
147
+ ss .add (s [j ]);
148
+ ans = Math .max (ans , j - i + 1 );
148
149
}
149
150
return ans ;
150
151
}
@@ -156,17 +157,17 @@ use std::collections::HashSet;
156
157
impl Solution {
157
158
pub fn length_of_longest_substring (s : String ) -> i32 {
158
159
let s = s . as_bytes ();
159
- let mut set = HashSet :: new ();
160
+ let mut ss = HashSet :: new ();
160
161
let mut i = 0 ;
161
162
s
162
163
. iter ()
163
164
. map (| c | {
164
- while set . contains (& c ) {
165
- set . remove (& s [i ]);
165
+ while ss . contains (& c ) {
166
+ ss . remove (& s [i ]);
166
167
i += 1 ;
167
168
}
168
- set . insert (c );
169
- set . len ()
169
+ ss . insert (c );
170
+ ss . len ()
170
171
})
171
172
. max ()
172
173
. unwrap_or (0 ) as i32
@@ -180,10 +181,9 @@ impl Solution {
180
181
* @return {number}
181
182
*/
182
183
var lengthOfLongestSubstring = function (s ) {
183
- const ss = new Set ();
184
- let i = 0 ;
185
184
let ans = 0 ;
186
- for (let j = 0 ; j < s .length ; ++ j) {
185
+ const ss = new Set ();
186
+ for (let i = 0 , j = 0 ; j < s .length ; ++ j) {
187
187
while (ss .has (s[j])) {
188
188
ss .delete (s[i++ ]);
189
189
}
@@ -197,12 +197,10 @@ var lengthOfLongestSubstring = function (s) {
197
197
``` cs
198
198
public class Solution {
199
199
public int LengthOfLongestSubstring (string s ) {
200
+ int ans = 0 ;
200
201
var ss = new HashSet <char >();
201
- int i = 0 , ans = 0 ;
202
- for (int j = 0 ; j < s .Length ; ++ j )
203
- {
204
- while (ss .Contains (s [j ]))
205
- {
202
+ for (int i = 0 , j = 0 ; j < s .Length ; ++ j ) {
203
+ while (ss .Contains (s [j ])) {
206
204
ss .Remove (s [i ++ ]);
207
205
}
208
206
ss .Add (s [j ]);
@@ -220,22 +218,16 @@ class Solution {
220
218
* @return Integer
221
219
*/
222
220
function lengthOfLongestSubstring($s) {
223
- $max = 0;
224
- for ($i = 0; $i < strlen($s); $i++) {
225
- $chars = [];
226
- $sub = '';
227
- for ($j = $i; $j < strlen($s); $j++) {
228
- if (in_array($s[$j], $chars)) {
229
- break;
230
- }
231
- $sub .= $s[$j];
232
- $chars[] = $s[$j];
233
- }
234
- if (strlen($sub) > $max) {
235
- $max = strlen($sub);
221
+ $ans = 0;
222
+ $ss = [];
223
+ for ($i = 0, $j = 0; $j < strlen($s); ++$j) {
224
+ while (in_array($s[$j], $ss)) {
225
+ unset($ss[array_search($s[$i++], $ss)]);
236
226
}
227
+ $ss[] = $s[$j];
228
+ $ans = max($ans, $j - $i + 1);
237
229
}
238
- return $max ;
230
+ return $ans ;
239
231
}
240
232
}
241
233
```
@@ -284,80 +276,4 @@ proc lengthOfLongestSubstring(s: string): int =
284
276
285
277
<!-- tabs: end -->
286
278
287
- ### 方法二
288
-
289
- <!-- tabs: start -->
290
-
291
- ``` java
292
- class Solution {
293
- public int lengthOfLongestSubstring (String s ) {
294
- boolean [] ss = new boolean [128 ];
295
- int ans = 0 , j = 0 ;
296
- int n = s. length();
297
- for (int i = 0 ; i < n; ++ i) {
298
- char c = s. charAt(i);
299
- while (ss[c]) {
300
- ss[s. charAt(j++ )] = false ;
301
- }
302
- ans = Math . max(ans, i - j + 1 );
303
- ss[c] = true ;
304
- }
305
- return ans;
306
- }
307
- }
308
- ```
309
-
310
- ``` cpp
311
- class Solution {
312
- public:
313
- int lengthOfLongestSubstring(string s) {
314
- bool ss[ 128] = {false};
315
- int n = s.size();
316
- int ans = 0;
317
- for (int i = 0, j = 0; i < n; ++i) {
318
- while (ss[ s[ i]] ) {
319
- ss[ s[ j++]] = false;
320
- }
321
- ss[ s[ i]] = true;
322
- ans = max(ans, i - j + 1);
323
- }
324
- return ans;
325
- }
326
- };
327
- ```
328
-
329
- ```go
330
- func lengthOfLongestSubstring(s string) (ans int) {
331
- ss := make([]bool, 128)
332
- j := 0
333
- for i, c := range s {
334
- for ss[c] {
335
- ss[s[j]] = false
336
- j++
337
- }
338
- ss[c] = true
339
- ans = max(ans, i-j+1)
340
- }
341
- return
342
- }
343
- ```
344
-
345
- ``` ts
346
- function lengthOfLongestSubstring(s : string ): number {
347
- let ans = 0 ;
348
- const n = s .length ;
349
- const ss: boolean [] = new Array (128 ).fill (false );
350
- for (let i = 0 , j = 0 ; i < n ; ++ i ) {
351
- while (ss [s [i ]]) {
352
- ss [s [j ++ ]] = false ;
353
- }
354
- ss [s [i ]] = true ;
355
- ans = Math .max (ans , i - j + 1 );
356
- }
357
- return ans ;
358
- }
359
- ```
360
-
361
- <!-- tabs: end -->
362
-
363
279
<!-- end -->
0 commit comments