[LeetCode] Positions of Large Groups 大群组的位置
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 singlelarge 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,是的话就将双指针位置存入结果res中,并更新指针,参见代码如下:
解法一:
class Solution {
public:
vector<vector<int>> largeGroupPositions(string S) {
vector<vector<int>> res;
int n = S.size(), i = , j = ;
while (j < n) {
while (j < n && S[j] == S[i]) ++j;
if (j - i >= ) res.push_back({i, j - });
i = j;
}
return res;
}
};
我们也可以换一种写法,不用while循环,而是使用for循环,但本质上还是双指针的思路,并没有什么太大的区别,参见代码如下:
解法二:
class Solution {
public:
vector<vector<int>> largeGroupPositions(string S) {
vector<vector<int>> res;
int n = S.size(), start = ;
for (int i = ; i <= n; ++i) {
if (i < n && S[i] == S[start]) continue;
if (i - start >= ) res.push_back({start, i - });
start = i;
}
return res;
}
};
参考资料:
https://leetcode.com/problems/positions-of-large-groups/
https://leetcode.com/problems/positions-of-large-groups/discuss/128961/Java-Solution-Two-Pointers
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Positions of Large Groups 大群组的位置的更多相关文章
- 830. Positions of Large Groups - LeetCode
Question 830. Positions of Large Groups Solution 题目大意: 字符串按连续相同字符分组,超过3个就返回首字符和尾字符 思路 : 举例abcdddeeee ...
- 830. Positions of Large Groups@python
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- 【Leetcode_easy】830. Positions of Large Groups
problem 830. Positions of Large Groups solution1: class Solution { public: vector<vector<int&g ...
- Positions of Large Groups
Positions of Large Groups In a string S of lowercase letters, these letters form consecutive groups ...
- [LeetCode] 839. Similar String Groups 相似字符串组
Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it ...
- C#LeetCode刷题之#830-较大分组的位置(Positions of Large Groups)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3750 访问. 在一个由小写字母构成的字符串 S 中,包含由一些连 ...
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
随机推荐
- C语言中 if 和 else if 的区别
先看代码:我们本意是对i不同的值有不同的判断. #include<stdio.h> int main(void) { ; ) { i++; printf("%d\n", ...
- 机器学习基石8-Noise and Error
注: 文章中所有的图片均来自台湾大学林轩田<机器学习基石>课程. 笔记原作者:红色石头 微信公众号:AI有道 上一节课,我们主要介绍了VC Dimension的概念.如果Hypothese ...
- 理解 Linux 的硬链接与软链接【转】
转自:https://www.ibm.com/developerworks/cn/linux/l-cn-hardandsymb-links/index.html 从 inode 了解 Linux 文件 ...
- 基于用户的协同过滤电影推荐user-CF python
协同过滤包括基于物品的协同过滤和基于用户的协同过滤,本文基于电影评分数据做基于用户的推荐 主要做三个部分:1.读取数据:2.构建用户与用户的相似度矩阵:3.进行推荐: 查看数据u.data 主要用到前 ...
- 两个fragment之间简单的跳转
1.在第一个fragment中开启事务,设置标记 Toast.makeText(getActivity(), "切换到下一个fragment中", Toast.LENGTH_SHO ...
- IIS+Tomcat功能iis端口2
之前写过IIS桥接Tomcat是通过isapi_redirect.dll,组件方式实现共用端口的,但是在Windows2012服务器 iis8.0版本中,配置完成后没有效果,比较抓狂,分析发现如下信息 ...
- net core体系-Standard-1概述
前言 早上起来.NET社区沸腾了,期待已久的.NET Core 2.0终于发布!根据个人经验,微软的产品一般在2.0时会趋于成熟,所以一个新的.Net开发时代已经来临!未来属于.NET Core. . ...
- zabbix-tomcat监控
安装tomcat 1安装jdk # yum install lrzsz -y #tar xvf jdk # ln -sv /usr/local/src/jdk1..0_79/ /usr/local/j ...
- Docker 学习5 Docker容器网络
一.内核网络名称空间 1.可通过ip netns进行操作 [root@localhost /]# ip netns help Usage: ip netns list ip netns add NAM ...
- python基础学习(一)--数据类型
Python一个 高级语言 2017-09-19 1.1 Python背景简介(感谢伟大的廖雪峰大佬带我们走上一条光头路,嘿嘿) 写了大半年Python代码,感觉收获不是很大,都是现学现卖,没有系统 ...