50
50
51
51
** 方法一:双指针 + 哈希表**
52
52
53
- 定义一个哈希表记录当前窗口内出现的字符,$i$, $j$ 分别表示不重复子串的开始位置和结束位置,$ans$ 表示无重复字符子串的最大长度 。
53
+ 定义一个哈希表记录当前窗口内出现的字符,记 $i$ 和 $j$ 分别表示不重复子串的开始位置和结束位置,无重复字符子串的最大长度记为 $ans$。
54
54
55
- 遍历 $s$ 每个字符 $c$, 若 $[ i, j - 1] $ 窗口内存在 $c$,则 $i$ 循环向右移动,更新哈希表,直至 $[ i, j - 1] $ 窗口不存在 $c$,循环结束。将 $c$ 加入哈希表中,此时 $[ i, j] $ 窗口内不含重复元素,更新 $ans$ 的最大值: $ans = max(ans, j - i + 1)$。
55
+ 遍历字符串 $s$ 的每个字符 $s [ j ] $,我们记为 $c$。 若 $s [ i..j- 1] $ 窗口内存在 $c$,则 $i$ 循环向右移动,更新哈希表,直至 $s [ i..j- 1] $ 窗口不存在 $c$,循环结束。将 $c$ 加入哈希表中,此时 $s [ i.. j] $ 窗口内不含重复元素,更新 $ans$ 的最大值 $ans = max(ans, j - i + 1)$。
56
56
57
57
最后返回 $ans$ 即可。
58
58
@@ -78,13 +78,13 @@ for (int i = 0, j = 0; i < n; ++i) {
78
78
``` python
79
79
class Solution :
80
80
def lengthOfLongestSubstring (self , s : str ) -> int :
81
+ ss = set ()
81
82
i = ans = 0
82
- chars = set ()
83
83
for j, c in enumerate (s):
84
- while c in chars :
85
- chars .remove(s[i])
84
+ while c in ss :
85
+ ss .remove(s[i])
86
86
i += 1
87
- chars .add(c)
87
+ ss .add(c)
88
88
ans = max (ans, j - i + 1 )
89
89
return ans
90
90
```
@@ -96,21 +96,64 @@ class Solution:
96
96
``` java
97
97
class Solution {
98
98
public int lengthOfLongestSubstring (String s ) {
99
- int i = 0 , j = 0 , ans = 0 ;
100
- Set<Character > chars = new HashSet<> ();
101
- for (char c : s. toCharArray()) {
102
- while (chars. contains(c)) {
103
- chars. remove(s. charAt(i++ ));
99
+ Set<Character > ss = new HashSet<> ();
100
+ int i = 0 , ans = 0 ;
101
+ for (int j = 0 ; j < s. length(); ++ j) {
102
+ char c = s. charAt(j);
103
+ while (ss. contains(c)) {
104
+ ss. remove(s. charAt(i++ ));
104
105
}
105
- chars . add(c);
106
+ ss . add(c);
106
107
ans = Math . max(ans, j - i + 1 );
107
- ++ j;
108
108
}
109
109
return ans;
110
110
}
111
111
}
112
112
```
113
113
114
+ ### ** C++**
115
+
116
+ ``` cpp
117
+ class Solution {
118
+ public:
119
+ int lengthOfLongestSubstring(string s) {
120
+ unordered_set<char > ss;
121
+ int i = 0, ans = 0;
122
+ for (int j = 0; j < s.size(); ++j) {
123
+ while (ss.count(s[ j] )) ss.erase(s[ i++] );
124
+ ss.insert(s[ j] );
125
+ ans = max(ans, j - i + 1);
126
+ }
127
+ return ans;
128
+ }
129
+ };
130
+ ```
131
+
132
+ ### **Go**
133
+
134
+ ```go
135
+ func lengthOfLongestSubstring(s string) int {
136
+ ss := map[byte]bool{}
137
+ i, ans := 0, 0
138
+ for j := 0; j < len(s); j++ {
139
+ for ss[s[j]] {
140
+ ss[s[i]] = false
141
+ i++
142
+ }
143
+ ss[s[j]] = true
144
+ ans = max(ans, j-i+1)
145
+ }
146
+ return ans
147
+ }
148
+
149
+ func max(a, b int) int {
150
+ if a > b {
151
+ return a
152
+ }
153
+ return b
154
+ }
155
+ ```
156
+
114
157
### ** JavaScript**
115
158
116
159
``` js
@@ -119,39 +162,56 @@ class Solution {
119
162
* @return {number}
120
163
*/
121
164
var lengthOfLongestSubstring = function (s ) {
122
- let i = 0 ,
123
- j = 0 ,
124
- ans = 0 ;
125
- let chars = new Set ();
126
- for (let c of s) {
127
- while (chars .has (c)) {
128
- chars .delete (s[i++ ]);
165
+ const ss = new Set ();
166
+ let i = 0 ;
167
+ let ans = 0 ;
168
+ for (let j = 0 ; j < s .length ; ++ j) {
169
+ while (ss .has (s[j])) {
170
+ ss .delete (s[i++ ]);
129
171
}
130
- chars .add (c );
172
+ ss .add (s[j] );
131
173
ans = Math .max (ans, j - i + 1 );
132
- ++ j;
133
174
}
134
175
return ans;
135
176
};
136
177
```
137
178
179
+ ### ** C#**
180
+
181
+ ``` cs
182
+ public class Solution {
183
+ public int LengthOfLongestSubstring (string s ) {
184
+ var ss = new HashSet <char >();
185
+ int i = 0 , ans = 0 ;
186
+ for (int j = 0 ; j < s .Length ; ++ j )
187
+ {
188
+ while (ss .Contains (s [j ]))
189
+ {
190
+ ss .Remove (s [i ++ ]);
191
+ }
192
+ ss .Add (s [j ]);
193
+ ans = Math .Max (ans , j - i + 1 );
194
+ }
195
+ return ans ;
196
+ }
197
+ }
198
+ ```
199
+
138
200
### ** TypeScript**
139
201
140
202
``` ts
141
203
function lengthOfLongestSubstring(s : string ): number {
142
- // 滑动窗口+哈希表
143
- let left = - 1 ;
144
- let maxLen = 0 ;
145
- let hashTable = new Map ();
146
- for (let right = 0 ; right < s .length ; right ++ ) {
147
- let cur = s .charAt (right );
148
- if (hashTable .has (cur )) {
149
- left = Math .max (left , hashTable .get (cur ));
204
+ const ss = new Set ();
205
+ let i = 0 ;
206
+ let ans = 0 ;
207
+ for (let j = 0 ; j < s .length ; ++ j ) {
208
+ while (ss .has (s [j ])) {
209
+ ss .delete (s [i ++ ]);
150
210
}
151
- hashTable . set ( cur , right );
152
- maxLen = Math .max (maxLen , right - left );
211
+ ss . add ( s [ j ] );
212
+ ans = Math .max (ans , j - i + 1 );
153
213
}
154
- return maxLen ;
214
+ return ans ;
155
215
}
156
216
```
157
217
@@ -179,56 +239,6 @@ class Solution {
179
239
}
180
240
```
181
241
182
- ### ** Go**
183
-
184
- ``` go
185
- func lengthOfLongestSubstring (s string ) int {
186
- window := make (map [byte ]int )
187
- n := len (s)
188
- ans := 0
189
- left , right := 0 , 0
190
- for right < n {
191
- b := s[right]
192
- right++
193
- window[b]++
194
- for window[b] > 1 {
195
- window[s[left]]--
196
- left++
197
- }
198
- ans = max (ans, right-left)
199
- }
200
- return ans
201
- }
202
-
203
- func max (x , y int ) int {
204
- if x > y {
205
- return x
206
- }
207
- return y
208
- }
209
- ```
210
-
211
- ### ** C++**
212
-
213
- ``` cpp
214
- class Solution {
215
- public:
216
- int lengthOfLongestSubstring(string s) {
217
- int i = 0, j = 0, ans = 0;
218
- unordered_set<char > chars;
219
- for (char& c : s)
220
- {
221
- while (chars.count(c)) chars.erase(s[ i++] );
222
- chars.insert(c);
223
- ans = max(ans, j - i + 1);
224
- ++j;
225
- }
226
- return ans;
227
-
228
- }
229
- };
230
- ```
231
-
232
242
### ** Nim**
233
243
234
244
``` nim
0 commit comments