【LeetCode】830. Positions of Large Groups 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/positions-of-large-groups/description/
题目描述
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
题目大意
一个长字符串可以按照字符的连续出现,分组。每个组内都是一段连续的,字符相同的子字符串。
要求,长度不小于3的所有组的字符串起始和结束位置。
解题方法
直接暴力求解即可!从左到右遍历字符串,只要后面的字符和该组起始的字符相同,那么就是属于同一个组;否则,开辟一个新组,并且判断之前的这个组长度是否>=3,是的话进行保存。
第一遍提交没通过的原因是忘了判断,当字符串结束的时候也是一个组终止的标志。比如字符串"aaa"
。
class Solution:
def largeGroupPositions(self, S):
"""
:type S: str
:rtype: List[List[int]]
"""
groups = []
before_index, before_char = 0, S[0]
for i, s in enumerate(S):
if s != before_char:
if i - before_index >= 3:
groups.append([before_index, i - 1])
before_index = i
before_char = s
if i - before_index >= 2:
groups.append([before_index, i])
return groups
二刷的时候,对结尾的判断是添加了一个大写字符,这样的话在不打扰之前小写字符串的基础上,增加了结束符号。
class Solution:
def largeGroupPositions(self, S):
"""
:type S: str
:rtype: List[List[int]]
"""
S = S + "A"
groups = []
previndex, prevc = 0, ""
for i, c in enumerate(S):
if not prevc:
prevc = c
previndex = i
elif prevc != c:
if i - previndex >= 3:
groups.append([previndex, i - 1])
previndex = i
prevc = c
return groups
日期
2018 年 5 月 27 日 ———— 周末的天气很好~
2018 年 11 月 16 日 —— 又到周五了!
【LeetCode】830. Positions of Large Groups 解题报告(Python)的更多相关文章
- 830. Positions of Large Groups - LeetCode
Question 830. Positions of Large Groups Solution 题目大意: 字符串按连续相同字符分组,超过3个就返回首字符和尾字符 思路 : 举例abcdddeeee ...
- 【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@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_Easy tag: Two Pointers
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- 830. Positions of Large Groups
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
随机推荐
- 脱离Editor、VS等IDE如何编译UE4工程
在Windows平台下,我们从.uproject文件生成VS解决方案.sln文件 .uproject文件用于打开Editor .sln文件用于打开VS工程 对于有增加C++代码的工程,Editor中和 ...
- Spring Boot 热启动插件
1. maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId ...
- 微信小程序的wx.login用async和data解决code不一致的问题
由于wx.login是异步函数,导致在我们获取微信小程序返回的code去请求我们的登录接口时code的值会异常.现在用promise封装一下,将他success的结果返回,在登陆函数中await就可以 ...
- Java8使用并行流(ParallelStream)注意事项
Java8并行流ParallelStream和Stream的区别就是支持并行执行,提高程序运行效率.但是如果使用不当可能会发生线程安全的问题.Demo如下: public static void co ...
- 集合类——Map集合、Properties属性文件操作
1.Map集合 Collection集合的特点是每次进行单个对象的保存,若要对一对对象来进行保存就只能用Map集合来保存.即Map集合中一次可以保存两个对象,且这两个对象的关系是key = value ...
- 【C/C++】string的长度
一般用 s.length() s.size() 两种 size也可以用于vector string和vector的区别 string输入直接cin vector一般类似压栈pushback 输入一般是 ...
- 在Eclipse中编写jQuery代码时产生的错误(连载)
1.Error:启动Eclipse中的服务,显示错误,端口号被占用 解决方法: 方式一:修改对应的端口号(实际情况实际处理) 方式二:在进程中关闭Eclispe重新打开即可(截图说明) 2.Error ...
- shell脚本 binlog方式增量备份mysql
一.简介 源码地址 日期:2018/4/12 介绍:复制Binlog日志方式的增量备份脚本,并保存固定天数的备份 效果图: 二.使用 适用:centos6+ 语言:中文 注意:使用前先修改脚本中变量 ...
- Kali渗透安卓手机
kali渗透安卓手机 1.生成木马文件 msfvenom -p android/meterpreter/reverse_tcp LHOST=ip LPORT=端口 R > test.apk 在终 ...
- LuoguP1619 解一元二次方程的烦恼 题解
Content 模拟一个系统,给出一个数 \(n\),让你判断是否是素数,如果是合数的话就要质因数分解. 需要注意的几点: 数字超过 \(4\times 10^7\),输出溢出提示. 数字小于 \(2 ...