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

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:

输入: "pwwkew"
输出: 3
解释: 无重复字符的最长子串是 "wke",其长度为 3。
     请注意,答案必须是一个子串,"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 }

原文地址:https://www.cnblogs.com/strengthen/p/9858659.html

时间: 2024-11-07 02:24:06

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

leetcode 3. 无重复字符的最长子串(Longest Substring Without Repeating Characters)

目录 题目描述: 示例 1: 示例 2: 示例 3: 解法: 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1. 示例 3: 输入: "pwwkew"

BAT面试算法进阶(2)- 无重复字符的最长子串(暴力法)

一.算法题 题目 Given a string, find the length of the longest substring without repeating characters. Example Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length

【Leetcode】【Longest Substring Without Repeating Characters】【无重复字符的最长子串】【C++】

题目:给定一字符串,求其无重复字符的最长子串长度. 思路:for循环一次,时间复杂度为O(N).字符的ascii值为32~126.start表示当前无重复字符子串的初始位置,初始值为0:可定义一个位置数组pos[128]表示for循环索引到当前位置时相应的字符对应的位置.若当前字符s[i](其ascii值为cur_pos),若pos[cur_pos]>=start,说明在start之后已有该字符s[i],则以start开始的子串第一次遇到重复字符,打住.判断当前长度是否大于max_len,若大于

代码题(56)— 最长重复子串、无重复字符的最长子串

1.最长的重复子串 寻找一个字符串中最长的重复子串 最大后缀方法思路: 1. 用字符串指针数组保存用户输入的字符串的所有后缀字符串: 2. 将后缀字符串集合进行排序: 3. 比较相邻字符串的公共子串长度,找到长度最大值,保存相应字符串即为所求 空间复杂度:求长度为n的字符串的后缀,需要O(n)的空间复杂度  时间复杂度:产生后缀数组-时间复杂度O(N).对后缀数组排序是O(N*NlogN),第一个N表示字符串的比较,后面NlogN使用快排排序.依次检测相邻两个后缀的公共长度-时间复杂度O(N*N

[LeetCode]无重复字符的最长子串

给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 3. 示例 2: 输入: "bbbbb" 输出: 1 解释: 无重复字符的最长子串是 "b",其长度为 1. 示例 3: 输入: "pwwkew" 输出: 3 解释: 无重复字符的最长子串是 "wke",其长度为 3.   请注

LeetCode 无重复字符的最长子串(探索字节跳动)

题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1. 示例 3: 输入: "pwwkew" 输出: 3 解释: 因为无重复字符的最长子串是 "w

领扣-无重复字符的最长子串-Python实现

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1. 示例 3: 输入: "pwwkew" 输出: 3 解释: 因为无重复字符的最长子串是 "wke&qu

LeetCode 第3题 无重复字符的最长子串

/*3. 无重复字符的最长子串给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. 示例 2: 输入: "bbbbb"输出: 1解释: 因为无重复字符的最长子串是 "b",所以其长度为 1. 示例 3: 输入: "pwwkew"输出: 3解释: 因为无重复字符的最长子串是 &qu

3. 无重复字符的最长子串

3. 无重复字符的最长子串 方法一 class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ str_dic = {} i, ans = 0, 0 for j in range(len(s)): if s[j] in str_dic: i = max(str_dic[s[j]], i) ans = max(ans,