mirror of https://github.com/doocs/leetcode.git
feat: add swift implementation to lcof problem: No.48 (#2933)
This commit is contained in:
parent
c0fb2eee7e
commit
a72988e1e2
|
|
@ -220,6 +220,30 @@ public class Solution {
|
|||
}
|
||||
```
|
||||
|
||||
#### Swift
|
||||
|
||||
```swift
|
||||
class Solution {
|
||||
func lengthOfLongestSubstring(_ s: String) -> Int {
|
||||
var ans = 0
|
||||
var j = 0
|
||||
var vis = Set<Character>()
|
||||
let sArray = Array(s)
|
||||
|
||||
for i in 0..<sArray.count {
|
||||
while vis.contains(sArray[i]) {
|
||||
vis.remove(sArray[j])
|
||||
j += 1
|
||||
}
|
||||
vis.insert(sArray[i])
|
||||
ans = max(ans, i - j + 1)
|
||||
}
|
||||
|
||||
return ans
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<!-- tabs:end -->
|
||||
|
||||
<!-- solution:end -->
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
class Solution {
|
||||
func lengthOfLongestSubstring(_ s: String) -> Int {
|
||||
var ans = 0
|
||||
var j = 0
|
||||
var vis = Set<Character>()
|
||||
let sArray = Array(s)
|
||||
|
||||
for i in 0..<sArray.count {
|
||||
while vis.contains(sArray[i]) {
|
||||
vis.remove(sArray[j])
|
||||
j += 1
|
||||
}
|
||||
vis.insert(sArray[i])
|
||||
ans = max(ans, i - j + 1)
|
||||
}
|
||||
|
||||
return ans
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue