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:

  1. Input: "abbxxxxzzy"
  2. Output: [[3,6]]
  3. Explanation: "xxxx" is the single large group with starting 3 and ending positions 6.

Example 2:

  1. Input: "abc"
  2. Output: []
  3. Explanation: We have "a","b" and "c" but no large group.

Example 3:

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

较大分组的位置

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

  1. class Solution(object):
  2. def largeGroupPositions(self, S):
  3. """
  4. :type S: str
  5. :rtype: List[List[int]]
  6. """
  7. last_item = None
  8. left_index = -1
  9. nums_list = []
  10. for i, item in enumerate(S + '#'):
  11. if last_item != item:
  12. if i - left_index > 2:
  13. nums_list.append([left_index, i - 1])
  14. left_index = i
  15. last_item = item
  16. 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. Maven私服仓库类型

    1. 代理仓库(Proxy Repository) 顾名思义是代理第三方仓库的,如: maven-central nuget.org-proxy 版本策略(Version Policy): Relea ...

  2. python中的clear

    1 a = { 2 "name":"dlrb", 3 "age":25, 4 "height":168 5 } 6 a. ...

  3. centos 7 端口

    查看端口是否占用 netstat -tlnp|grep 8080 查看已经开放的端口 firewall-cmd --zone=public --list-ports 增加开放端口 firewall-c ...

  4. 20165304 实验二 Java面向对象程序设计

    一.面向对象程序设计1--单元测试和TDD 实验要求 1.参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习 2 ...

  5. django for 循环中,获取序号

    模板的for循环中,如何获取序号? 想过用enumerate,但是在模板中会报错 Could not parse the remainder xxx: 后来搜到 forloop.counter,完美解 ...

  6. WPF ListView即时更新

    1.ListView 的 ItemSource 使用 BindingList < T >: 注:由于 List < T > 没有实现 INotifyPropertyChange ...

  7. 07-border(边框)

    边框 边框有三个要素: 粗细.线性样式.颜色 border: solid 如果颜色不写,默认是黑色. 如果粗细不写,不显示边框. 如果只写线性样式,默认的有上下左右 3px的宽度,实体样式,并且黑色的 ...

  8. vue-x action 的相互调用

    实现方式:分发 Action Action 通过 store.dispatch 方法触发: store.dispatch('increment')

  9. LeetCode OJ 143. Reorder List(两种方法,快慢指针,堆栈)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  10. Android 操作UI线程的一些方法

    我们经常会在后台线程中去做一些耗时的操作,比如去网络取数据.但是当数据取回来,需要显示到页面上的时候,会遇到一些小麻烦,因为我们都知道,android的UI页面是不允许在其他线程直接操作的.下面总结4 ...