,敢教日月换新天。为有牺牲多壮志

[Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9858659.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.


给定一个字符串,找出不含有重复字符的最长子串的长度。

示例 1:

输入: "abcabcbb"
输出: 3 
解释: 无重复字符的最长子串是 "abc",其长度为 3。

示例 2:

输入: "bbbbb"
输出: 1
解释: 无重复字符的最长子串是 "b",其长度为 1。

示例 3:

"wke"
"pwke"

36ms
复制代码
 1 class Solution {
 2     func lengthOfLongestSubstring(_ s: String) -> Int {
 3         var m:[Int] = [Int](repeating: -1,count: 256)
 4         var res:Int = 0, left:Int = -1
 5         for i in 0..<s.count
 6         {
 7             let num:Int = s[s.index(s.startIndex,offsetBy: i)].toInt()
 8             left = max(left,m[num])
 9             m[num] = i
10             res = max(res,i - left)
11         }
12         return res
13     }
14 }
15 //Character扩展方法  
16 extension Character  
17 {  
18     func toInt() -> Int  
19     {  
20         var num:Int = Int()
21         for scalar in String(self).unicodeScalars  
22         {  
23             num = Int(scalar.value)  
24         }  
25         return num  
26     }  
27 }
复制代码

20ms

复制代码
 1 class Solution {
 2  func lengthOfLongestSubstring(_ s: String) -> Int {
 3      var right = 1
 4      var left = 0
 5      var i = 0
 6      var result = 0
 7      
 8      if s.count > 0 {
 9          result = right - left
10          let chars = Array(s.utf8)
11          
12          //Interate in a incremental window 
13          while right < chars.count {
14              i = left
15                 while i < right {
16                     //Check if a duplicate is found
17                     if chars[i] == chars[right] {
18                         left = i + 1
19                         break
20                     } 
21                 i = i + 1
22              }
23          result = max(result,right-left+1)     
24          right = right + 1
25      }
26      }
27      return result
28      }
29 }
复制代码

28ms

复制代码
 1 class Solution {
 2  func lengthOfLongestSubstring(_ s: String) -> Int {
 3         if s.characters.count == 0 {
 4             return 0
 5         }
 6         
 7         let chars = Array(s.utf8)
 8         var left = 0
 9         var right = 1
10         var result = right - left
11         var i = 0
12         
13         while right < chars.count {
14             i = left
15             while i < right {
16                 if chars[i] == chars[right] {
17                     left = i + 1
18                     break
19                 }
20                 i += 1
21             }
22             result = max(result, right - left + 1)
23             right += 1
24         }
25         
26         return result
27     }
28  }
复制代码

36ms

复制代码
 1 class Solution {
 2     func lengthOfLongestSubstring(_ s: String) -> Int {
 3         var arr = [Int](repeating: -1, count: 256)
 4         var s = Array(s)
 5         var currentLength = 0
 6         var maxLength = 0
 7         for i in 0..<s.count {
 8             let cValue = Int(s[i].unicodeScalars.first!.value)
 9             let preIndex = arr[cValue]
10             if arr[cValue] == -1 || i - preIndex > currentLength {
11                 currentLength += 1
12             } else {
13                 maxLength = max(currentLength, maxLength)
14                 currentLength = i - preIndex
15             }
16             arr[cValue] = i
17         }
18         
19         maxLength = max(currentLength, maxLength)
20         return maxLength
21     }
22 }
复制代码

44ms

复制代码
 1 class Solution {
 2     func lengthOfLongestSubstring(_ s: String) -> Int {
 3         
 4         var last:[Int] = Array.init(repeating: -1, count: 127)
 5         var start: Int = 0 // 起始位置
 6         var maxLength: Int = 0  // 最大长度
 7         
 8         let chars: [Int8]? = s.cString(using: String.Encoding.utf8)
 9         
10         for i in 0..<s.count {
11             
12             let hash_i: Int = Int(chars![i])
13             let hash_null: Int = 0
14             
15             if (last[hash_i - hash_null] >= start) {
16                 maxLength = max(maxLength, i-start)
17                 start = last[hash_i - hash_null] + 1
18             }
19             last[hash_i - hash_null] = i
20         }
21         maxLength = max(maxLength, s.count - start)
22         return maxLength
23     }
24 }
复制代码

 

52ms

复制代码
 1 class Solution {
 2     func lengthOfLongestSubstring(_ s: String) -> Int {
 3         let charArr = Array(s)
 4         let len = s.count
 5         var ans = 0
 6         var index = Array.init(repeating: 0, count: 128)
 7         var j = 0
 8         for i in 0..<len {
 9             let char = charArr[i]
10             if let v = char.unicodeScalars.first?.value {
11                 j = max(index[Int(v)], j)
12                 ans = max(ans, i - j + 1)
13                 index[Int(v)] = i + 1
14             }
15         }
16         return ans
17     }
18 }
复制代码

84ms

复制代码
 1 class Solution {
 2     func lengthOfLongestSubstring(_ s: String) -> Int {
 3         let cs = s.cString(using: .utf8)
 4         guard var chars = cs else { return 0 }
 5         guard chars.count > 1 else {
 6             return 0
 7         }
 8         chars.removeLast()
 9         var maxCount: Int = 0
10         var i = 0, j = 0
11         var map: [Int8: Int] = [:]
12         while j < chars.count {
13             if map.keys.contains(chars[j]) {
14                 i = max(map[chars[j]] ?? 0, i)
15             }
16             maxCount = max(maxCount, j - i + 1)
17             map[chars[j]] = j + 1
18             j += 1
19         }
20         return maxCount
21     }
22 }
复制代码

 

posted @   为敢技术  阅读(718)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°