• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode830.较大分组的位置|PositionsofLargeGroups

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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

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

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

In a string S of lowercase letters, these letters form consecutive groups of the same character.

For example, a string like S = "abbxxxxzyy"has the groups "a""bb""xxxx""z"and "yy".

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: "xxxx" is the single large group with starting  3 and ending positions 6.

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 <= S.length <= 1000


在一个由小写字母构成的字符串 S 中,包含由一些连续的相同字符所构成的分组。

例如,在字符串 S = "abbxxxxzyy" 中,就含有 "a""bb""xxxx""z" 和 "yy" 这样的一些分组。

我们称所有包含大于或等于三个连续字符的分组为较大分组。找到每一个较大分组的起始和终止位置。

最终结果按照字典顺序输出。

示例 1:

输入: "abbxxxxzzy"
输出: [[3,6]]
解释: "xxxx" 是一个起始于 3 且终止于 6 的较大分组

示例 2:

输入: "abc"
输出: []
解释: "a","b" 和 "c" 均不是符合要求的较大分组。

示例 3:

输入: "abcdddeeeeaabbbcd"
输出: [[3,5],[6,9],[12,14]]

说明:  1 <= S.length <= 1000


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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[Swift]LeetCode881.救生艇|BoatstoSavePeople发布时间:2022-07-13
下一篇:
[Swift]LeetCode837.新21点|New21Game发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap