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

https://leetcode.com/problems/positions-of-large-groups/discuss/128942/My-Easy-7-Lines-C%2B%2B-Solution

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Positions of Large Groups 大群组的位置的更多相关文章

  1. 830. Positions of Large Groups - LeetCode

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

  2. 830. Positions of Large Groups@python

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

  3. 【Leetcode_easy】830. Positions of Large Groups

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

  4. Positions of Large Groups

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

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

  6. C#LeetCode刷题之#830-较大分组的位置(Positions of Large Groups)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3750 访问. 在一个由小写字母构成的字符串 S 中,包含由一些连 ...

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

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

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

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

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

随机推荐

  1. 基于注解的SpringMVC添加其他的Servlet、Filter以及Listener

    我们可以在AbstractAnnotationConfigDispatcherServletInitializer的实现类中重写onStartup(ServletContext servletCont ...

  2. gitlab 权限说明

    五.权限说明 Guest(匿名用户) - 创建项目.写留言薄 Reporter(报告人)- 创建项目.写留言薄.拉项目.下载项目.创建代码片 段 Developer(开发者)- 创建项目.写留言薄.拉 ...

  3. MySQL Server 的安装方法及简要步骤

    闲扯两句: 小弟不才,由于缺乏明确的职业规划,初毕业的那两年从事的是网络管理工作,接触最多的是计算机硬件和网络设备. 近几年才开始转向DB行业,最初是自学,过程中走了不少弯路,后来参加的专职的DBA培 ...

  4. luoguP1373 小a和uim之大逃离

    DP专题 题目链接 思路 \(f[i][j][a][b][0/1]\)表示在\((i,j)\)这个格子,小a有a滴魔液,他的伙伴有b滴,上一步是小a(0)或者他的伙伴(1)吸取的魔液. (显然)数组开 ...

  5. UiAutomator2.0 - 控件实现点击操作原理

    目录 一.UiObject 二.UiObject2 穿梭各大技术博客网站,每天都能看到一些的新的技术.突然感觉UiAutomator 2.0相对于现在来说已经是个很久远的东西了ε=(´ο`*))).写 ...

  6. table td中的内容过长,显示为固定长度,多余部分用省略号显示

    简单描述:table数据过长,结果顶到下一格,影响了数据的查看 解决办法: 给table 加上style属性   另外 给td加上style标签修饰 <table class="tab ...

  7. spring MVC 项目 WEB-INF下的jsp不能加载css文件

    一.项目目录 二.解决方法(已解决) 1. jsp文件加入 <link href="<c:url value="/css/main.css" />&qu ...

  8. node 和 npm 常用命令

    npm node 简述 快速入门 安装npm和管理npm版本 npm安装 更新npm npm -v npm install npm@latest -g npm install npm@next -g ...

  9. Mybatis 源码学习系列

    前言 很久以前,我们学习了Java,从一个控制台的 Hello world .开始,我们进入了面向对象的世界. 然后由学习了SQL语言,可以写出SQL语句来将尘封在硬盘之下的数据库数据,展现出来. 后 ...

  10. windows安装node和yarn

    Ubuntu子系统安装和删除yarn 在 Debian 或 Ubuntu 上,需要用 Debian 包仓库来安装 Yarn. 首先需要配置仓库: curl -sS https://dl.yarnpkg ...