File tree Expand file tree Collapse file tree 3 files changed +74
-0
lines changed
lcof/面试题48. 最长不含重复字符的子字符串 Expand file tree Collapse file tree 3 files changed +74
-0
lines changed Original file line number Diff line number Diff line change @@ -151,6 +151,48 @@ var lengthOfLongestSubstring = function (s) {
151
151
};
152
152
```
153
153
154
+ ### ** TypeScript**
155
+
156
+ ``` ts
157
+ function lengthOfLongestSubstring(s : string ): number {
158
+ let res = 0 ;
159
+ let l = 0 ;
160
+ let set = new Set <string >();
161
+ for (const c of s ) {
162
+ while (set .has (c )) {
163
+ set .delete (s [l ++ ]);
164
+ }
165
+ set .add (c );
166
+ res = Math .max (res , set .size );
167
+ }
168
+ return res ;
169
+ }
170
+ ```
171
+
172
+ ### ** Rust**
173
+
174
+ ``` rust
175
+ use std :: collections :: HashSet ;
176
+
177
+ impl Solution {
178
+ pub fn length_of_longest_substring (s : String ) -> i32 {
179
+ let mut res = 0 ;
180
+ let mut i = 0 ;
181
+ let mut set = HashSet :: new ();
182
+ let chars = s . chars (). collect :: <Vec <char >>();
183
+ for c in chars . iter () {
184
+ while set . contains (c ) {
185
+ set . remove (& chars [i ]);
186
+ i += 1 ;
187
+ }
188
+ set . insert (c );
189
+ res = res . max (set . len ())
190
+ }
191
+ res as i32
192
+ }
193
+ }
194
+ ```
195
+
154
196
### ** ...**
155
197
156
198
```
Original file line number Diff line number Diff line change
1
+ use std:: collections:: HashSet ;
2
+
3
+ impl Solution {
4
+ pub fn length_of_longest_substring ( s : String ) -> i32 {
5
+ let mut res = 0 ;
6
+ let mut i = 0 ;
7
+ let mut set = HashSet :: new ( ) ;
8
+ let chars = s. chars ( ) . collect :: < Vec < char > > ( ) ;
9
+ for c in chars. iter ( ) {
10
+ while set. contains ( c) {
11
+ set. remove ( & chars[ i] ) ;
12
+ i += 1 ;
13
+ }
14
+ set. insert ( c) ;
15
+ res = res. max ( set. len ( ) )
16
+ }
17
+ res as i32
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ function lengthOfLongestSubstring ( s : string ) : number {
2
+ let res = 0 ;
3
+ let l = 0 ;
4
+ let set = new Set < string > ( ) ;
5
+ for ( const c of s ) {
6
+ while ( set . has ( c ) ) {
7
+ set . delete ( s [ l ++ ] ) ;
8
+ }
9
+ set . add ( c ) ;
10
+ res = Math . max ( res , set . size ) ;
11
+ }
12
+ return res ;
13
+ }
You can’t perform that action at this time.
0 commit comments