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]]

较大分组的位置

关键位置在前后元素不同时的处理,索引需要重置,并判断重复次数.字符串末尾增加一个#字符是为了处理末尾满足重复次数大于三的情况.

class Solution(object):
def largeGroupPositions(self, S):
"""
:type S: str
:rtype: List[List[int]]
"""
last_item = None
left_index = -1
nums_list = []
for i, item in enumerate(S + '#'):
if last_item != item:
if i - left_index > 2:
nums_list.append([left_index, i - 1])
left_index = i
last_item = item
return nums_list

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

  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. 830. Positions of Large Groups@python

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

  4. [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 ...

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

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

  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. a.每个 HTML 文件里开头都有个<!DOCTYPE>

    <!DOCTYPE> 位于文档中最前面的位置,告诉浏览器以哪个HTML版本进行解析. 在 HTML5 中只有一种:<!DOCTYPE html> .

  2. 《算法》第三章部分程序 part 3

    ▶ 书中第三章部分程序,加上自己补充的代码,红黑树 ● 红黑树,大部分方法与注释与二叉树相同 package package01; import java.util.NoSuchElementExce ...

  3. Mybatis八( mybatis工作原理分析)

    MyBatis的主要成员 Configuration        MyBatis所有的配置信息都保存在Configuration对象之中,配置文件中的大部分配置都会存储到该类中 SqlSession ...

  4. Intorduction of Annotation ,about examples in Hello1.java

    java ee 里的常用注解 .@Controller,@Service,@Repository,@Component,@RestController这几个注解用于实例化class对象.分别对应于控制 ...

  5. docker pull centos慢问题的解决方案

    1.现象 如果直接docker pull centos 两个小时才down下来8M,很慢 2.解决 [root@localhost network-scripts]# cd /etc/docker [ ...

  6. 小柒2012 / spring-boot-quartz

    spring-boot-quartz 基于spring-boot+quartz的CRUD动态任务管理系统,适用于中小项目. 基于spring-boot 2.x +quartz 的CRUD任务管理系统: ...

  7. Mp4 to Img

    # -*- coding: utf-8 -*- """ Created on Thu May 3 16:51:50 2018 """ # 录 ...

  8. treeMap 基于JDK 1.8的学习

    困惑了很久的红黑树========来个了断吧 本文主要是为了描述旋转的原则,所以,至于红黑树的数据结构,红黑树的基本准则,不在强调,,红黑树困惑的就是这旋转的过程.!!! 画红黑树很简单的画图工具: ...

  9. cxgrid中回车键光标移到下列

    OptionsBehavior.GoToNextCellOnEnter:=True; 更完善的回车 可以在焦点到了最后一列再回车时有下一行则移到下一行的第一列,没有下一行则新增记录并移到第一列 pro ...

  10. Dos命令快速设置ip、网关、dns地址

    netsh interface ip set address name="本地连接" source=static 192.168.1.8 255.255.255.0 192.168 ...