在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ In a string For example, a string like Call a group large if it has 3 or more characters. We would like the starting and ending positions of every large group. The final answer should be in lexicographic order. Example 1: Input: "abbxxxxzzy"
Output: [[3,6]]
Explanation:
Example 2: Input: "abc" Output: [] Explanation: We have "a","b" and "c" but no large group. Example 3: Input: "abcdddeeeeaabbbcd" Output: [[3,5],[6,9],[12,14]] Note: 在一个由小写字母构成的字符串 例如,在字符串 我们称所有包含大于或等于三个连续字符的分组为较大分组。找到每一个较大分组的起始和终止位置。 最终结果按照字典顺序输出。 示例 1: 输入: "abbxxxxzzy"
输出: [[3,6]]
解释:
示例 2: 输入: "abc" 输出: [] 解释: "a","b" 和 "c" 均不是符合要求的较大分组。 示例 3: 输入: "abcdddeeeeaabbbcd" 输出: [[3,5],[6,9],[12,14]] 说明: 16ms 1 class Solution { 2 func largeGroupPositions(_ S: String) -> [[Int]] { 3 var left = 0 4 let chars = Array(S) 5 var result = [[Int]]() 6 for i in 0..<chars.count { 7 if chars[left] == chars[i] { 8 continue 9 } else { 10 if i - left >= 3 { 11 result.append([left, i - 1]) 12 } 13 left = i 14 } 15 } 16 if chars.count - left >= 3 { 17 result.append([left, chars.count - 1]) 18 } 19 return result 20 } 21 } 20ms 1 class Solution { 2 func largeGroupPositions(_ S: String) -> [[Int]] { 3 let chars = S.utf8CString 4 var char = chars[0] 5 var count = 1,start = 0 6 var location: [[Int]] = [] 7 for i in 1..<chars.count { 8 if char == chars[i] { 9 count += 1 10 } else { 11 if count >= 3 { 12 location.append([start, start+count-1]) 13 } 14 char = chars[i] 15 start = i 16 count = 1 17 } 18 } 19 return location 20 } 21 } 20ms 1 class Solution { 2 func largeGroupPositions(_ S: String) -> [[Int]] { 3 var result = [[Int]]() 4 var array = Array(S) 5 6 if array.count <= 2 { 7 return result 8 } 9 10 var i = 2 11 while i < array.count { 12 var start = -1 13 if array[i] == array[i - 1] && array[i - 1] == array[i - 2] { 14 start = i - 2 15 } 16 if start == -1 { 17 i += 1 18 } 19 else { 20 i += 1 21 while i < array.count && array[i] == array[start] { 22 i += 1 23 } 24 result.append([start, i - 1]) 25 } 26 } 27 return result 28 } 29 } 24ms 1 class Solution { 2 func largeGroupPositions(_ S: String) -> [[Int]] { 3 let A = Array(S) 4 var mem: [[Int]] = [] 5 var begin = 0 6 var element = A[0] 7 for (index, item) in A.enumerated() { 8 if(item == element){ 9 continue 10 } else { 11 let gap = index-begin 12 if(gap>=3){ 13 let temp = [begin, index-1] 14 mem.append(temp) 15 } 16 begin = index 17 element = item 18 } 19 } 20 21 if(begin != -1 && A.count - begin >= 3){ 22 mem.append([begin, A.count-1]) 23 } 24 return mem 25 } 26 } 36ms 1 class Solution { 2 func largeGroupPositions(_ S: String) -> [[Int]] { 3 var chars = Array(S) 4 let count = S.count 5 var res = [[Int]]() 6 var i = 0 7 8 for j in 0..<count { 9 if j == count - 1 || chars[j] != chars[j + 1] { 10 if j - i + 1 >= 3 { 11 res.append([i, j]) 12 } 13 i = j + 1 14 } 15 } 16 17 return res 18 } 19 } 48ms 1 class Solution { 2 func largeGroupPositions(_ S: String) -> [[Int]] { 3 var currentStart = 0 4 var currentEnd = 0 5 var currentChar: Character = "\n" 6 var result = [[Int]]() 7 for (index,char) in S.enumerated() { 8 if char != currentChar { 9 if currentEnd - currentStart >= 2 { 10 result.append([currentStart,currentEnd]) 11 } 12 currentStart = index 13 currentEnd = index 14 currentChar = char 15 } else { 16 currentEnd = index 17 } 18 } 19 if currentEnd - currentStart >= 2 { 20 result.append([currentStart,currentEnd]) 21 } 22 return result 23 } 24 }
|
请发表评论