作者: 负雪明烛
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.

  1. Example 1:
  2. Input: "abbxxxxzzy"
  3. Output: [[3,6]]
  4. Explanation: "xxxx" is the single large group with starting 3 and ending positions 6.
  5. Example 2:
  6. Input: "abc"
  7. Output: []
  8. Explanation: We have "a","b" and "c" but no large group.
  9. Example 3:
  10. Input: "abcdddeeeeaabbbcd"
  11. Output: [[3,5],[6,9],[12,14]]

Note: 1 <= S.length <= 1000

题目大意

一个长字符串可以按照字符的连续出现,分组。每个组内都是一段连续的,字符相同的子字符串。

要求,长度不小于3的所有组的字符串起始和结束位置。

解题方法

直接暴力求解即可!从左到右遍历字符串,只要后面的字符和该组起始的字符相同,那么就是属于同一个组;否则,开辟一个新组,并且判断之前的这个组长度是否>=3,是的话进行保存。

第一遍提交没通过的原因是忘了判断,当字符串结束的时候也是一个组终止的标志。比如字符串"aaa"

  1. class Solution:
  2. def largeGroupPositions(self, S):
  3. """
  4. :type S: str
  5. :rtype: List[List[int]]
  6. """
  7. groups = []
  8. before_index, before_char = 0, S[0]
  9. for i, s in enumerate(S):
  10. if s != before_char:
  11. if i - before_index >= 3:
  12. groups.append([before_index, i - 1])
  13. before_index = i
  14. before_char = s
  15. if i - before_index >= 2:
  16. groups.append([before_index, i])
  17. return groups

二刷的时候,对结尾的判断是添加了一个大写字符,这样的话在不打扰之前小写字符串的基础上,增加了结束符号。

  1. class Solution:
  2. def largeGroupPositions(self, S):
  3. """
  4. :type S: str
  5. :rtype: List[List[int]]
  6. """
  7. S = S + "A"
  8. groups = []
  9. previndex, prevc = 0, ""
  10. for i, c in enumerate(S):
  11. if not prevc:
  12. prevc = c
  13. previndex = i
  14. elif prevc != c:
  15. if i - previndex >= 3:
  16. groups.append([previndex, i - 1])
  17. previndex = i
  18. prevc = c
  19. return groups

日期

2018 年 5 月 27 日 ———— 周末的天气很好~
2018 年 11 月 16 日 —— 又到周五了!

【LeetCode】830. Positions of Large Groups 解题报告(Python)的更多相关文章

  1. 830. Positions of Large Groups - LeetCode

    Question 830. Positions of Large Groups Solution 题目大意: 字符串按连续相同字符分组,超过3个就返回首字符和尾字符 思路 : 举例abcdddeeee ...

  2. 【Leetcode_easy】830. Positions of Large Groups

    problem 830. Positions of Large Groups solution1: class Solution { public: vector<vector<int&g ...

  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_Easy tag: Two Pointers

    In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...

  6. 830. Positions of Large Groups

    In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...

  7. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  8. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  9. 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...

随机推荐

  1. SpringBoot整合Shiro 四:认证+授权

    搭建环境见: SpringBoot整合Shiro 一:搭建环境 shiro配置类见: SpringBoot整合Shiro 二:Shiro配置类 shiro整合Mybatis见:SpringBoot整合 ...

  2. MybatisPlus的CRUD及拓展

    创建一个简单的MybatisPlus项目在上一篇博客:MybatisPlus入门程序 一.CRUD 1. select 1.1 查找全部用户 //查 @Test public void select( ...

  3. 强化学习实战 | 自定义Gym环境之井字棋

    在文章 强化学习实战 | 自定义Gym环境 中 ,我们了解了一个简单的环境应该如何定义,并使用 print 简单地呈现了环境.在本文中,我们将学习自定义一个稍微复杂一点的环境--井字棋.回想一下井字棋 ...

  4. day03 部署NFS服务

    day03 部署NFS服务 NFS的原理 1.什么是NFS 共享网络文件存储服务器 2.NFS的原理 1.用户访问NFS客户端,将请求转化为函数 2.NFS通过TCP/IP连接服务端 3.NFS服务端 ...

  5. 浏览器相关,关于强缓存、协商缓存、CDN缓存。

    强缓存和协商缓存 在介绍缓存的时候,我们习惯将缓存分为强缓存和协商缓存两种.两者的主要区别是使用本地缓存的时候,是否需要向服务器验证本地缓存是否依旧有效. 顾名思义,协商缓存,就是需要和服务器进行协商 ...

  6. Data Calendar

    1.Date对象 Date类在java.util包中.使用Date类的无参数构造方法创建的对象可以获取本地当前时间. 用Date的构造方法Date(long time)创建的Date对象表 示相对19 ...

  7. docker之镜像制作

    #:下载镜像并初始化系统 root@ubuntu:~# docker pull centos #:创建目录 root@ubuntu:/opt# mkdir dockerfile/{web/{nginx ...

  8. 【Linux】【Services】【Project】Haproxy Keepalived Postfix实现邮件网关Cluster

    1. 简介: 1.1. 背景:公司使用exchange服务器作为邮件服务器,但是使用Postfix作为邮件网关实现病毒检测,内容过滤,反垃圾邮件等功能.原来的架构非常简单,只有两台机器,一个负责进公司 ...

  9. Properties类继承HashTable类,一般用来给程序配置属性文件。

    package com.itcast.demo04.Prop;import jdk.internal.util.xml.impl.ReaderUTF8;import sun.nio.cs.UTF_32 ...

  10. MyBatis绑定Mapper接口参数到Mapper映射文件sql语句参数

    一.设置paramterType 1.类型为基本类型 a.代码示例 映射文件: <select id="findShopCartInfoById" parameterType ...