【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 ...
随机推荐
- Linux三剑客之老三grep
说明: Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.工作中我们常常用它来过滤出我们想要的数据. 格式: grep [OPTIONS] 基本参 ...
- Java日期时间操作基础——包含JDK1.8时间操作新特性
JDK1.7日期时间操作 示例小结 public class DateTest { public static final String FORMAT_DATE = "yyyy-MM-dd& ...
- SpringBoot Profiles 多环境配置及切换
目录 前言 默认环境配置 多环境配置 多环境切换 小结 前言 大部分情况下,我们开发的产品应用都会根据不同的目的,支持运行在不同的环境(Profile)下,比如: 开发环境(dev) 测试环境(tes ...
- A Child's History of England.49
But he was shipwrecked in the Adriatic Sea, and was fain [happy, willing] to pass through Germany, u ...
- CentOS7 搭建maven私服Nexus
下载解压 官网https://www.sonatype.com/download-oss-sonatype 下载页面 https://help.sonatype.com/repomanager2/do ...
- css系列,选择器权重计算方式
CSS选择器分基本选择器(元素选择器,类选择器,通配符选择器,ID选择器,关系选择器), 属性选择器,伪类选择器,伪元素选择器,以及一些特殊选择器,如has,not等. 在CSS中,权重决定了哪些CS ...
- 【JAVA开发】浅析双亲委派机制
双亲委派机制存在的意义 双亲委派只是一种说法,个人觉得叫单亲委派更合适,因为向上传递的父类只有一个,估计只是翻译过来的,形成的一种习惯,大家可以当做单亲委派 四种加载器都是用于类的加载,只是加载的对象 ...
- 【♪♪♪】网易云音乐mp3真实地址
参考别人的博客,得到下面的地址,填上ID号即可,后缀的[.mp3]不用输入 http://music.163.com/song/media/outer/url?id= 例如 最终,合并地址为 http ...
- Spring Cloud声明式调用Feign负载均衡FeignClient详解
为了深入理解Feign,下面将从源码的角度来讲解Feign.首先来看看FeignClient注解@FeignClient的源码,代码如下: FeignClient注解被@Target(ElementT ...
- mybatis-插件开发
在Executor.StatementHandler.parameterHandler.resultSetHandler创建的时候都有一步这样的操作xxxHandler=interceptorChai ...