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. git工具——对比文件的不同

    对比工作区和某个版本中文件的不同: (1)继续编辑文件code.txt,在其中添加一行内容: (2)现在要对比工作区中code.txt和head版本中code.txt的不同,使用如下命令: 前面没有出 ...

  2. .bat以管理员身份运行

    原文地址:https://blog.csdn.net/stranger_hello/article/details/82257947 @echo off :获取管理员权限 %1 mshta vbscr ...

  3. Gitlab_ansible_jenkins三剑客⑤jenkins Pipeline-job的使用

    Pipeline-job的使用 创建Pipeline任务 找到root用户的id 编写pipeline脚本 #!groovy pipeline{ agent {node {label 'master' ...

  4. 04mycat数据切分

    自定义切分文件 [root@mycat mycat]# cat conf/customer-hash-int.txt 101=0 102=0 103=0 104=1 105=1 106=1 Rule. ...

  5. 五 Zabbix全网监控

    监控的作用 我们的职责   1.保障企业数据的安全可靠.   2.为客户提供7*24小时服务.   3.不断提升用户的体验.在关键时刻,提前提醒我们服务器要出问题了当出问题之后,可以便于找到问题的根源 ...

  6. Docker----在Docker中部署Asp.net core2.1以及修改发布

    本篇文章主要是如何在Docker容器中运行ASP.NET Core应用程序,以及修改系统之后,发布更新.本文章采用自定义的Docker文件 系统要求: 1.服务器或本地已经安装docker 一.创建一 ...

  7. 饮冰三年-人工智能-Python-27 Django Form组件

    Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显示内容 后台逻辑(导包+建类) from django ...

  8. 【玩转开源】BananaPi R2 —— 第二篇 Openwrt 网口配置分析

    上次和大家分享了如何烧录和安装Openwrt到BananaPi R2,运行Openwrt的R2目前就具备路由器的功能了,这次我们来看看R2运行Openwrt的性能如何,同时也会讲解一些常用的网络知识. ...

  9. Java线段树

    线段树不是完全二叉树,是平衡二叉树 堆也是平衡二叉树 堆满二叉树: h层,一共有2^h-1个节点(大约是2^h) 最后一层(h-1层)有2^(h-1)个节点 最后一层的节点数大致等于前面所有层节点之和 ...

  10. EFCore+Mysql仓储层建设(分页、多字段排序、部分字段更新)

    前沿 园子里已有挺多博文介绍了EFCore+Mysql/MSSql如何进行使用,但实际开发不会把EF层放在Web层混合起来,需要多个项目配合结构清晰的进行分层工作,本文根据个人实践经验总结将各个项目进 ...