Skip to content

Commit a72988e

Browse files
authored
feat: add swift implementation to lcof problem: No.48 (#2933)
1 parent c0fb2ee commit a72988e

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lcof/面试题48. 最长不含重复字符的子字符串/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,30 @@ public class Solution {
220220
}
221221
```
222222

223+
#### Swift
224+
225+
```swift
226+
class Solution {
227+
func lengthOfLongestSubstring(_ s: String) -> Int {
228+
var ans = 0
229+
var j = 0
230+
var vis = Set<Character>()
231+
let sArray = Array(s)
232+
233+
for i in 0..<sArray.count {
234+
while vis.contains(sArray[i]) {
235+
vis.remove(sArray[j])
236+
j += 1
237+
}
238+
vis.insert(sArray[i])
239+
ans = max(ans, i - j + 1)
240+
}
241+
242+
return ans
243+
}
244+
}
245+
```
246+
223247
<!-- tabs:end -->
224248

225249
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
func lengthOfLongestSubstring(_ s: String) -> Int {
3+
var ans = 0
4+
var j = 0
5+
var vis = Set<Character>()
6+
let sArray = Array(s)
7+
8+
for i in 0..<sArray.count {
9+
while vis.contains(sArray[i]) {
10+
vis.remove(sArray[j])
11+
j += 1
12+
}
13+
vis.insert(sArray[i])
14+
ans = max(ans, i - j + 1)
15+
}
16+
17+
return ans
18+
}
19+
}

0 commit comments

Comments
 (0)