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 ...
随机推荐
- [Xcode 实际操作]二、视图与手势-(2)UIView视图的层次关系
目录:[Swift]Xcode实际操作 本文将演示创建三个视图对象,其中第二个视图是第三个视图的父视图. 现在开始编写代码,实现这项功能 import UIKit class ViewControll ...
- [Xcode 实际操作]九、实用进阶-(24)使用Segue(页面的跳转连接)进行页面跳转并传递参数
目录:[Swift]Xcode实际操作 本文将演示使用Segue(页面的跳转连接)进行页面跳转并传递参数. 参照结合:[Xcode10 实际操作]九.实用进阶-(23)多个Storyboard故事板中 ...
- Luogu P1514引水入城【搜索】 By cellur925
题目传送门 这道题开始看好像并没有什么思路,和搜索好像也并没有什么关系.但是我们手玩下样例就会发现,思路其实就三句话:(写这道题的时候在代码里写的) //我们想知道从第1行的每列往下到干旱区的范围 / ...
- Eclipse 修改编码方式
1.修改全局(Eclipse默认)文本编码方式 2.修改某个工程编码方式 右键工程点击”属性“后,如下图
- 51Nod 1099 任务执行顺序 (贪心)
#include <iostream> #include <algorithm> using namespace std; +; struct node{ int r, q; ...
- properties 文件注意事项
不要使用""双引号包裹内容 db.validationQuery="select 1"比如上面这种是错误的,下面的是正确的写法 db.validationQue ...
- SpringBoot | Hibernate @Transient 注解
在默认情况下,持久化类的所有属性会自动映射到数据表的数据列.如果在实际应用中,不想持久保存某些属性,则可以考虑使用@Transient来修饰它们. 如果一个属性并非数据库表的字段映射,就务必将其标示为 ...
- 【模板】平衡树——Treap和Splay
二叉搜索树($BST$):一棵带权二叉树,满足左子树的权值均小于根节点的权值,右子树的权值均大于根节点的权值.且左右子树也分别是二叉搜索树.(如下) $BST$的作用:维护一个有序数列,支持插入$x$ ...
- AKOJ-2010-魔法石
链接:https://oj.ahstu.cc/JudgeOnline/problem.php?id=2010 题意: Vyoung最近收集到一大批魔法石,这些魔法石有两种特性,攻击和防守,不同特性的两 ...
- 2017"百度之星"程序设计大赛 - 资格赛 寻找母串
Problem Description 对于一个串S,当它同时满足如下条件时,它就是一个01偏串: 1.只由0和1两种符组成: 2.在S的每一个前缀中,0的个数不超过1的个数: 3.S中0的个数和1的 ...