File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed
lcof2/剑指 Offer II 016. 不含重复字符的最长子字符串 Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -197,6 +197,29 @@ function lengthOfLongestSubstring(s: string): number {
197
197
}
198
198
```
199
199
200
+ #### Swift
201
+
202
+ ``` swift
203
+ class Solution {
204
+ func lengthOfLongestSubstring (_ s : String ) -> Int {
205
+ var ss = Array (repeating : false , count : 128 )
206
+ var ans = 0
207
+ var j = s.startIndex
208
+
209
+ for i in s.indices {
210
+ let c = s[i]
211
+ while ss[Int (c.asciiValue ! )] {
212
+ ss[Int (s[j].asciiValue ! )] = false
213
+ j = s.index (after : j)
214
+ }
215
+ ans = max (ans, s.distance (from : j, to : i) + 1 )
216
+ ss[Int (c.asciiValue ! )] = true
217
+ }
218
+ return ans
219
+ }
220
+ }
221
+ ```
222
+
200
223
<!-- tabs: end -->
201
224
202
225
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func lengthOfLongestSubstring( _ s: String ) -> Int {
3
+ var ss = Array ( repeating: false , count: 128 )
4
+ var ans = 0
5
+ var j = s. startIndex
6
+
7
+ for i in s. indices {
8
+ let c = s [ i]
9
+ while ss [ Int ( c. asciiValue!) ] {
10
+ ss [ Int ( s [ j] . asciiValue!) ] = false
11
+ j = s. index ( after: j)
12
+ }
13
+ ans = max ( ans, s. distance ( from: j, to: i) + 1 )
14
+ ss [ Int ( c. asciiValue!) ] = true
15
+ }
16
+ return ans
17
+ }
18
+ }
You can’t perform that action at this time.
0 commit comments