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: "abcdddeeeeaabbbcd"
  2. Output: [[3,5],[6,9],[12,14]]

原题地址: Positions of Large Groups

题意: 将数组分组,标记每一组的首尾索引值,返回分组长度等于3的索引值

难度: Easy

思路:

(1)遍历数组并用count数组标记,注意首尾边界

(2)遍历count,找出长度大于等于3的分组

代码:

  1. class Solution(object):
  2. def largeGroupPositions(self, S):
  3. """
  4. :type S: str
  5. :rtype: List[List[int]]
  6. """
  7. count = []
  8. i = 0
  9. j = 1
  10.  
  11. while j <= len(S):
  12. if j == len(S):
  13. count.append([i, j-1])
  14. break
  15.  
  16. if S[i] == S[j]:
  17. j += 1
  18. else:
  19. count.append([i, j-1])
  20. i = j
  21.  
  22. res = []
  23. for c in count:
  24. if c[1] - c[0] + 1 >= 3:
  25. res.append(c)
  26. return res

时间复杂度: O(n)

空间复杂度: O(n)

在上面的基础上优化一下,一次变量即可

代码:

  1. class Solution(object):
  2. def largeGroupPositions(self, S):
  3. """
  4. :type S: str
  5. :rtype: List[List[int]]
  6. """
  7. res = []
  8. i, j = 0, 1
  9. n = len(S)
  10. while j < n:
  11. if S[j] != S[j-1]: # 找到不相等的值
  12. count = j - i
  13. if count >= 3:
  14. res.append([i, j-1])
  15. i = j
  16. j += 1
  17.  
  18. if j - i >= 3: # 处理边界问题
  19. res.append([i, j-1])
  20.  
  21. return res

830. Positions of Large Groups@python的更多相关文章

  1. 【Leetcode_easy】830. Positions of Large Groups

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

  2. 830. Positions of Large Groups - LeetCode

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

  3. [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 ...

  4. 【LeetCode】830. Positions of Large Groups 解题报告(Python)

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

  5. 830. Positions of Large Groups

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

  6. Positions of Large Groups

    Positions of Large Groups In a string S of lowercase letters, these letters form consecutive groups ...

  7. [LeetCode] Positions of Large Groups 大群组的位置

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

  8. [Swift]LeetCode830. 较大分组的位置 | Positions of Large Groups

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

  9. [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 ...

随机推荐

  1. C#字体字号的改变

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  2. Sublime Text 报“Pylinter could not automatically determined the path to lint.py

    Pylinter could not automatically determined the path to lint.py. please provide one in the settings ...

  3. 1-zookeeper基本原理和使用

    1 分布式应用 1.1 分布式系统原理 在一个网络中,每台服务器上各跑一个应用,然后彼此连接起来就组成一套系统.比如提供完成的游戏服务,需要有认证应用,道具应用,积分应用,游戏主应用等,应用并非跑在一 ...

  4. 2017 Multi-University Training Contest - Team 1 Balala Power!

    Talented Mr.Tang has n strings consisting of only lower case characters. He wants to charge them wit ...

  5. Curious Array Codeforces - 407C(高阶差分(?)) || sequence

    https://codeforces.com/problemset/problem/407/C (自用,勿看) 手模一下找一找规律,可以发现,对于一个修改(l,r,k),相当于在[l,r]内各位分别加 ...

  6. MDX之Case When用法

    with member [Measures].[终端销售数量总计] as sum(ytd([日期].[年月].CurrentMember),[Measures].[终端销售数量]) member [M ...

  7. arcgis【0基础 】【1】 中如何添加MXD

    1,第一种方法 MapControl  直接添加 if (!axMapControl1.CheckMxFile(FileName)) { MessageBox.Show("文件不合法&quo ...

  8. BS3 多级菜单

    <div class="container"> <div class="row"> <h2>Multi level drop ...

  9. spring mvc 获取所有注册的url

    背景:坑货同事写代码一点规范都没有,瞎写,根据url没法直接定位到方法,无奈产生了此接口,程序员何苦为难程序员呢 @Autowired private RequestMappingHandlerMap ...

  10. java 并发容器一之BoundedConcurrentHashMap(基于JDK1.8)

    最近开始学习java并发容器,以补充自己在并发方面的知识,从源码上进行.如有不正确之处,还请各位大神批评指正. 前言: 本人个人理解,看一个类的源码要先从构造器入手,然后再看方法.下面看Bounded ...