【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 ...
随机推荐
- SpringBoot整合Shiro 四:认证+授权
搭建环境见: SpringBoot整合Shiro 一:搭建环境 shiro配置类见: SpringBoot整合Shiro 二:Shiro配置类 shiro整合Mybatis见:SpringBoot整合 ...
- MybatisPlus的CRUD及拓展
创建一个简单的MybatisPlus项目在上一篇博客:MybatisPlus入门程序 一.CRUD 1. select 1.1 查找全部用户 //查 @Test public void select( ...
- 强化学习实战 | 自定义Gym环境之井字棋
在文章 强化学习实战 | 自定义Gym环境 中 ,我们了解了一个简单的环境应该如何定义,并使用 print 简单地呈现了环境.在本文中,我们将学习自定义一个稍微复杂一点的环境--井字棋.回想一下井字棋 ...
- day03 部署NFS服务
day03 部署NFS服务 NFS的原理 1.什么是NFS 共享网络文件存储服务器 2.NFS的原理 1.用户访问NFS客户端,将请求转化为函数 2.NFS通过TCP/IP连接服务端 3.NFS服务端 ...
- 浏览器相关,关于强缓存、协商缓存、CDN缓存。
强缓存和协商缓存 在介绍缓存的时候,我们习惯将缓存分为强缓存和协商缓存两种.两者的主要区别是使用本地缓存的时候,是否需要向服务器验证本地缓存是否依旧有效. 顾名思义,协商缓存,就是需要和服务器进行协商 ...
- Data Calendar
1.Date对象 Date类在java.util包中.使用Date类的无参数构造方法创建的对象可以获取本地当前时间. 用Date的构造方法Date(long time)创建的Date对象表 示相对19 ...
- docker之镜像制作
#:下载镜像并初始化系统 root@ubuntu:~# docker pull centos #:创建目录 root@ubuntu:/opt# mkdir dockerfile/{web/{nginx ...
- 【Linux】【Services】【Project】Haproxy Keepalived Postfix实现邮件网关Cluster
1. 简介: 1.1. 背景:公司使用exchange服务器作为邮件服务器,但是使用Postfix作为邮件网关实现病毒检测,内容过滤,反垃圾邮件等功能.原来的架构非常简单,只有两台机器,一个负责进公司 ...
- Properties类继承HashTable类,一般用来给程序配置属性文件。
package com.itcast.demo04.Prop;import jdk.internal.util.xml.impl.ReaderUTF8;import sun.nio.cs.UTF_32 ...
- MyBatis绑定Mapper接口参数到Mapper映射文件sql语句参数
一.设置paramterType 1.类型为基本类型 a.代码示例 映射文件: <select id="findShopCartInfoById" parameterType ...