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:

Input: "abcdddeeeeaabbbcd"
Output: [[3,5],[6,9],[12,14]]

原题地址: Positions of Large Groups

题意: 将数组分组,标记每一组的首尾索引值,返回分组长度等于3的索引值

难度: Easy

思路:

(1)遍历数组并用count数组标记,注意首尾边界

(2)遍历count,找出长度大于等于3的分组

代码:

class Solution(object):
def largeGroupPositions(self, S):
"""
:type S: str
:rtype: List[List[int]]
"""
count = []
i = 0
j = 1 while j <= len(S):
if j == len(S):
count.append([i, j-1])
break if S[i] == S[j]:
j += 1
else:
count.append([i, j-1])
i = j res = []
for c in count:
if c[1] - c[0] + 1 >= 3:
res.append(c)
return res

时间复杂度: O(n)

空间复杂度: O(n)

在上面的基础上优化一下,一次变量即可

代码:

class Solution(object):
def largeGroupPositions(self, S):
"""
:type S: str
:rtype: List[List[int]]
"""
res = []
i, j = 0, 1
n = len(S)
while j < n:
if S[j] != S[j-1]: # 找到不相等的值
count = j - i
if count >= 3:
res.append([i, j-1])
i = j
j += 1 if j - i >= 3: # 处理边界问题
res.append([i, j-1]) return res

830. Positions of Large Groups@python的更多相关文章

  1. 【Leetcode_easy】830. Positions of Large Groups

    problem 830. Positions of Large Groups solution1: class Solution { public: vector<vector<int&g ...

  2. 830. Positions of Large Groups - LeetCode

    Question 830. Positions of Large Groups Solution 题目大意: 字符串按连续相同字符分组,超过3个就返回首字符和尾字符 思路 : 举例abcdddeeee ...

  3. [LeetCode&Python] Problem 830. Positions of Large Groups

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

  4. 【LeetCode】830. Positions of Large Groups 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 830. Positions of Large Groups

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

  6. Positions of Large Groups

    Positions of Large Groups In a string S of lowercase letters, these letters form consecutive groups ...

  7. [LeetCode] Positions of Large Groups 大群组的位置

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

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

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

  9. [LeetCode] 830. Positions of Large Groups_Easy tag: Two Pointers

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

随机推荐

  1. IT兄弟连 JavaWeb教程 JSP内置对象经典面试题

    1.请说明cookie.request.session.application的作用域和声明周期? 并说明它们适用与什么场景? request的生命周期是一次请求.可以用于JSP表单提交数据. ses ...

  2. JS实现购物车动态功能

    整理了一下当时学js写的一些案例,觉得购物车功能在一般网站比较常见且基础,现在把它整理出来,需要的小伙伴可以参考一下. 该案例购物车主要功能如下: 1. 商品单选.全选.反选功能 2. 商品添加.删除 ...

  3. 关于MySQL集群架构优劣势与适用场景的分析与思考

    http://blog.itpub.net/25723371/viewspace-1977389/

  4. css margin padding 四个方向

    margin 和padding虽然有四个单独的方向属性,如margin-left,padding-bottom等等. 但是可以用margin:2px 3px 4px 5px     四个参数的含义:上 ...

  5. TTM-To the moon

    传送门 查询历史版本,回到历史版本,这个题目显然是用主席树,好像就没了! 但是这里的修改是区间修改,众所周知主席树的空间复杂度是\(nlog(n)\)的,区间修改会导致主席树的开点到达一个相当恐怖的数 ...

  6. shell学习(1)

    #!/bin/bash #######read从屏幕输入######## echo "what is your name?" read PERSON echo "Hell ...

  7. VLAN-6-VLAN Trunk协议(VTP)

    VTP能够将VLAN配置信息通告给邻居交换机,这样做可以使工程师只在一台交换机上配置VLAN,同一个VTP域中的所有其他交换机动态学习这些VLAN信息.VTP通告VLAN ID.VLAN 名称和 VL ...

  8. LOJ6212(博弈论)

    n <= L 和 n <= 2L 情况显然,一次就能取完: 分析 n > 2L 时: Alice手速太快,Bob同学是弱势群体,所以Bob肯定不能单纯地模仿Alice,那样顶多是平手 ...

  9. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) B

    Sherlock has a new girlfriend (so unlike him!). Valentine's day is coming and he wants to gift her s ...

  10. matplotlib 知识点13:绘制散点图(scatter函数精讲)

    散点图是指在回归分析中,数据点在直角坐标系平面上的分布图,散点图表示因变量随自变量而变化的大致趋势,据此可以选择合适的函数对数据点进行拟合. 用两组数据构成多个坐标点,考察坐标点的分布,判断两变量之间 ...