830. Positions of Large Groups@python
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:
- Input: "abcdddeeeeaabbbcd"
- Output: [[3,5],[6,9],[12,14]]
原题地址: Positions of Large Groups
题意: 将数组分组,标记每一组的首尾索引值,返回分组长度等于3的索引值
难度: Easy
思路:
(1)遍历数组并用count数组标记,注意首尾边界
(2)遍历count,找出长度大于等于3的分组
代码:
- class Solution(object):
- def largeGroupPositions(self, S):
- """
- :type S: str
- :rtype: List[List[int]]
- """
- count = []
- i = 0
- j = 1
- while j <= len(S):
- if j == len(S):
- count.append([i, j-1])
- break
- if S[i] == S[j]:
- j += 1
- else:
- count.append([i, j-1])
- i = j
- res = []
- for c in count:
- if c[1] - c[0] + 1 >= 3:
- res.append(c)
- return res
时间复杂度: O(n)
空间复杂度: O(n)
在上面的基础上优化一下,一次变量即可
代码:
- class Solution(object):
- def largeGroupPositions(self, S):
- """
- :type S: str
- :rtype: List[List[int]]
- """
- res = []
- i, j = 0, 1
- n = len(S)
- while j < n:
- if S[j] != S[j-1]: # 找到不相等的值
- count = j - i
- if count >= 3:
- res.append([i, j-1])
- i = j
- j += 1
- if j - i >= 3: # 处理边界问题
- res.append([i, j-1])
- return res
830. Positions of Large Groups@python的更多相关文章
- 【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 - LeetCode
Question 830. Positions of Large Groups Solution 题目大意: 字符串按连续相同字符分组,超过3个就返回首字符和尾字符 思路 : 举例abcdddeeee ...
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 830. Positions of Large Groups
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- Positions of Large Groups
Positions of Large Groups In a string S of lowercase letters, these letters form consecutive groups ...
- [LeetCode] Positions of Large Groups 大群组的位置
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- [Swift]LeetCode830. 较大分组的位置 | 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 ...
随机推荐
- C#字体字号的改变
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- 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 ...
- 1-zookeeper基本原理和使用
1 分布式应用 1.1 分布式系统原理 在一个网络中,每台服务器上各跑一个应用,然后彼此连接起来就组成一套系统.比如提供完成的游戏服务,需要有认证应用,道具应用,积分应用,游戏主应用等,应用并非跑在一 ...
- 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 ...
- Curious Array Codeforces - 407C(高阶差分(?)) || sequence
https://codeforces.com/problemset/problem/407/C (自用,勿看) 手模一下找一找规律,可以发现,对于一个修改(l,r,k),相当于在[l,r]内各位分别加 ...
- MDX之Case When用法
with member [Measures].[终端销售数量总计] as sum(ytd([日期].[年月].CurrentMember),[Measures].[终端销售数量]) member [M ...
- arcgis【0基础 】【1】 中如何添加MXD
1,第一种方法 MapControl 直接添加 if (!axMapControl1.CheckMxFile(FileName)) { MessageBox.Show("文件不合法&quo ...
- BS3 多级菜单
<div class="container"> <div class="row"> <h2>Multi level drop ...
- spring mvc 获取所有注册的url
背景:坑货同事写代码一点规范都没有,瞎写,根据url没法直接定位到方法,无奈产生了此接口,程序员何苦为难程序员呢 @Autowired private RequestMappingHandlerMap ...
- java 并发容器一之BoundedConcurrentHashMap(基于JDK1.8)
最近开始学习java并发容器,以补充自己在并发方面的知识,从源码上进行.如有不正确之处,还请各位大神批评指正. 前言: 本人个人理解,看一个类的源码要先从构造器入手,然后再看方法.下面看Bounded ...