![[Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters,第1张 [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters,第1张](/aiimages/%5BSwift%5DLeetCode3.+%E6%97%A0%E9%87%8D%E5%A4%8D%E5%AD%97%E7%AC%A6%E7%9A%84%E6%9C%80%E9%95%BF%E5%AD%90%E4%B8%B2+%7C+Longest+Substring+Without+Repeating+Characters.png)
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 解释: 无重复字符的最长子串是 长度为 3。"abc",其
示例 2:
输入: "bbbbb"输出: 1解释: 无重复字符的最长子串是 ,其长度为 1。"b"
示例 3:
输入: "pwwkew"输出: 3解释: 无重复字符的最长子串是 ,其长度为 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] = i10 res = max(res,i - left)11 }12 return res13 }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 - left10 let chars = Array(s.utf8)11 12 //Interate in a incremental window 13 while right < chars.count {14 i = left15 while i < right {16 //Check if a duplicate is found17 if chars[i] == chars[right] {18 left = i + 119 break20 } 21 i = i + 122 }23 result = max(result,right-left+1) 24 right = right + 125 }26 }27 return result28 }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 = 110 var result = right - left11 var i = 012 13 while right < chars.count {14 i = left15 while i < right {16 if chars[i] == chars[right] {17 left = i + 118 break19 }20 i += 121 }22 result = max(result,right - left + 1)23 right += 124 }25 26 return result27 }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 += 112 } else {13 maxLength = max(currentLength,maxLength)14 currentLength = i - preIndex15 }16 arr[cValue] = i17 }18 19 maxLength = max(currentLength,maxLength)20 return maxLength21 }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 = 014 15 if (last[hash_i - hash_null] >= start) {16 maxLength = max(maxLength,i-start)17 start = last[hash_i - hash_null] + 118 }19 last[hash_i - hash_null] = i20 }21 maxLength = max(maxLength,s.count - start)22 return maxLength23 }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 + 114 }15 }16 return ans17 }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 = 010 var i = 0,j = 011 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 + 118 j += 119 }20 return maxCount21 }22 }总结
以上是内存溢出为你收集整理的[Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters全部内容,希望文章能够帮你解决[Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)