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. springboot中使用kindeditor富文本编辑器实现博客功能

    kindeditor在之前已经用过,现在在springboot项目中使用.并且也在里面使用了图片上传以及回显等功能. 其实主要的功能是图片的处理:kindeditor对输入的内容会作为html标签处理 ...

  2. 论文翻译:BinaryNet: Training Deep Neural Networks with Weights and Activations Constrained to +1 or −1

    目录 摘要 引言 1.BinaryNet 符号函数 梯度计算和累积 通过离散化传播梯度 一些有用的成分 算法1 使用BinaryNet训练DNN 算法2 批量标准化转换(Ioffe和Szegedy,2 ...

  3. LeetCode第十九题-链表节点的删除

    Remove Nth Node From End of List 问题简介;给定链表,从链表末尾删除第n个节点并返回其头部 例: 给定链表:1-> 2-> 3-> 4-> 5, ...

  4. hihocoder 1505

    hihocoder 1505 题意:给你n个数,让你从n个数中抽两个数,再抽两个数,使得前两个数和后两个数相等 分析:对 i,j,p,q遍历的话时间复杂度会达到o(n4),所以考虑优化p,q 假设分配 ...

  5. dos.orm的事务处理

    dos.orm也包含事务处理,没有太多封装,这里有几个简单的示例代码. using (DbTrans trans = DbSession.Default.BeginTransaction()) { D ...

  6. Pompholyx - Causes, Symptoms and Treatment

    Pompholyx is a common type of eczema. It is also known as dyshidrotic eczema or vesicular eczema of ...

  7. 【MySQL】MySQL内连接,左连接,右连接查询

    概念 INNER JOIN(内连接):获取两个表中字段匹配关系的记录.也就是只会返回共有的内容. LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录. RIGHT JOIN(右 ...

  8. 《剑指offer》和为S的连续正数序列

    本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:

  9. 消息队列(MQ)

    1. 分类: 获取消息方式:A. push(推)方式:优点——可以尽可能快地将消息发送给消费者,缺点——如果消费者处理能力跟不上,消费者的缓冲区可能会溢出:     B. pull(拉)方式:优点—— ...

  10. sql查找某一列中某一数值出现次数大于3的记录的前3条

    SELECT * FROM table  GROUP BY column HAVING COUNT(column)>=3 ORDER BY column DESC LIMIT 0,3;