leetCode - 无重复字符的最长子串(Swift实现)

要求:给定一个字符串,请你找出其中不含有重复字符的最长子串的长度

 1 class Solution {
 2     func lengthOfLongestSubstring(_ s: String) -> Int {
 3        var left = 0
 4         var right = 0
 5         var dic : [String : Int] = [:]
 6         let characters : [String] = s.map{String($0)}
 7         var lenths : [Int] = []
 8         
 9         for (index, value) in characters.enumerated() {
10             if dic.keys.contains(value) && left <= dic[value]! {
11                 left = dic[value]! + 1
12             }
13             dic.updateValue(index, forKey: value)
14             right = index
15             lenths.append(right - left + 1)
16         }
17         return lenths.max() ?? 0
18     }
19 }

猜你喜欢

转载自www.cnblogs.com/kaisi/p/10130996.html
今日推荐