830. Positions of Large Groups
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的更多相关文章
- 【Leetcode_easy】830. Positions of Large Groups
problem 830. Positions of Large Groups solution1: class Solution { public: vector<vector<int&g ...
- 830. Positions of Large Groups - LeetCode
Question 830. Positions of Large Groups Solution 题目大意: 字符串按连续相同字符分组,超过3个就返回首字符和尾字符 思路 : 举例abcdddeeee ...
- 830. Positions of Large Groups@python
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- [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 ...
- 【LeetCode】830. Positions of Large Groups 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Positions of Large Groups
Positions of Large Groups In a string S of lowercase letters, these letters form consecutive groups ...
- [LeetCode] Positions of Large Groups 大群组的位置
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- [Swift]LeetCode830. 较大分组的位置 | Positions of Large Groups
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- [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 ...
随机推荐
- Maven私服仓库类型
1. 代理仓库(Proxy Repository) 顾名思义是代理第三方仓库的,如: maven-central nuget.org-proxy 版本策略(Version Policy): Relea ...
- python中的clear
1 a = { 2 "name":"dlrb", 3 "age":25, 4 "height":168 5 } 6 a. ...
- centos 7 端口
查看端口是否占用 netstat -tlnp|grep 8080 查看已经开放的端口 firewall-cmd --zone=public --list-ports 增加开放端口 firewall-c ...
- 20165304 实验二 Java面向对象程序设计
一.面向对象程序设计1--单元测试和TDD 实验要求 1.参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECUNITTEST 完成单元测试的学习 2 ...
- django for 循环中,获取序号
模板的for循环中,如何获取序号? 想过用enumerate,但是在模板中会报错 Could not parse the remainder xxx: 后来搜到 forloop.counter,完美解 ...
- WPF ListView即时更新
1.ListView 的 ItemSource 使用 BindingList < T >: 注:由于 List < T > 没有实现 INotifyPropertyChange ...
- 07-border(边框)
边框 边框有三个要素: 粗细.线性样式.颜色 border: solid 如果颜色不写,默认是黑色. 如果粗细不写,不显示边框. 如果只写线性样式,默认的有上下左右 3px的宽度,实体样式,并且黑色的 ...
- vue-x action 的相互调用
实现方式:分发 Action Action 通过 store.dispatch 方法触发: store.dispatch('increment')
- 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 ...
- Android 操作UI线程的一些方法
我们经常会在后台线程中去做一些耗时的操作,比如去网络取数据.但是当数据取回来,需要显示到页面上的时候,会遇到一些小麻烦,因为我们都知道,android的UI页面是不允许在其他线程直接操作的.下面总结4 ...