[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 single
large 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 ...
随机推荐
- python捕获异常及方法总结
调试Python程序时,经常会报出一些异常,异常的原因一方面可能是写程序时由于疏忽或者考虑不全造成了错误,这时就需要根据异常Traceback到出错点,进行分析改正:另一方面,有些异常是不可避免的,但 ...
- Linux input系统数据上报流程【转】
转自:https://segmentfault.com/a/1190000017255939 作为鸡生蛋系列文章,这里主要关注Linux input系统,主要为触摸事件上报流程. 读该文章最好有对li ...
- bently addin 二次开发学习
元素结构: 一些基本元素的添加与绘制: class CreateElement { public static void LineAndLineString() { Application app = ...
- 一场由like引发的事故
故事背景: 有一张用户级表,数据量在千万级别,而运营人员要查看这张表,其中有一项查询条件为根据“错误类型”(单值)查出所有包含这个类型的数据,而这个数据类型在数据库存放的方式类似于 “1,2,3,4, ...
- 设置SecureCRT的背景色和文字颜色方案
一.对于临时设置,可以如下操作: 首先options -- session - appearance 此处可以设置临时的窗口背景,字体颜色,大小等等,为什么说是临时,是因为只要你关闭连接后,又会恢复. ...
- MyCat全局表和ER--笔记(三)
全局表 全局表的作用 在分片的情况下,当业务表因为规模而进行分片以后,业务表与这些附属的字典表之间的关联,就成了比较棘手的问题,考虑到字典表具有以下几个特性: 变动不频繁 数据量总体变化不大 数据规模 ...
- 简单使用sp_executesql 参数化
declare @totalCount1 int output declare @id1 varchar(10) declare @strsql1 nvarchar(max)=N'' declare ...
- selenium操作浏览器的前进和后退
前进关键字:driver.forward() 后退关键字:driver.back() 测试对象:1.https://www.baidu.com/ 2.https://www.sogou.com/ 实例 ...
- [sublime] 利用sublime搭建C/C++编译器
gcc/g++配置 先去下载TDM-GCC安装包,这里附下载地址(可能会有弹出界面,不用管他). 现在c盘中建立文件夹 g++,然后以管理员运行,点击Create傻瓜式安装, 这里要改一下安装路径,保 ...
- js基础知识易错点(一)
最近替另一个项目招人,要求基础知识好,随便问了一些基础题,发现了一些易错的点,总结一下. 1.判断一个空数组 var arr = []; 1)JSON.stringify(arr) == " ...