Skip to content

Commit 8b47f48

Browse files
authored
feat: add swift implementation to lcof2 problem: No.016 (doocs#3004)
1 parent df08c12 commit 8b47f48

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lcof2/剑指 Offer II 016. 不含重复字符的最长子字符串/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,29 @@ function lengthOfLongestSubstring(s: string): number {
197197
}
198198
```
199199

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+
200223
<!-- tabs:end -->
201224

202225
<!-- solution:end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

0 commit comments

Comments
 (0)