• [Swift]LeetCode830. 较大分组的位置 | Positions of Large Groups


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

    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 = "
    "
     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 }
  • 相关阅读:
    ThreadLocal、InheritableThreadLocal、TransmittableThreadLocal使用及原理解析
    Spring Cloud入门教程
    极简策略模式
    MySQL热点行更新
    OpenResty安装、启动脚本及Lua脚本调试
    Spring Cloud Netflix超时时间设置
    SpringCloudCommons模块
    [转]Spring Boot @EnableConfigurationProperties @ConfigurationProperties注解配置原理源码分析
    SpringCloudContext模块
    [转]Spring源码解析之@Configuration
  • 原文地址:https://www.cnblogs.com/strengthen/p/10570916.html
Copyright © 2020-2023  润新知