在一个由小写字母构成的字符串 S 中,包含由一些连续的相同字符所构成的分组。

例如,在字符串 S = "abbxxxxzyy" 中,就含有 "a", "bb", "xxxx", "z" 和 "yy" 这样的一些分组。

我们称所有包含大于或等于三个连续字符的分组为较大分组。找到每一个较大分组的起始和终止位置。

最终结果按照字典顺序输出。

示例 1:

输入: "abbxxxxzzy" 输出: [[3,6]] 解释: "xxxx" 是一个起始于 3 且终止于 6 的较大分组。

示例 2:

输入: "abc" 输出: [] 解释: "a","b" 和 "c" 均不是符合要求的较大分组。

示例 3:

输入: "abcdddeeeeaabbbcd" 输出: [[3,5],[6,9],[12,14]]

说明:  1 <= S.length <= 1000

class Solution {
public:
vector<vector<int> > largeGroupPositions(string S) {
vector<vector<int> > res;
int len = S.size();
if(len == 0)
return res;
int start = 0;
int end = 0;
char flag = S[0];
for(int i = 1; i < len; i++)
{
end = i;
if(S[i] != flag)
{
end = i - 1;
if(end - start + 1 >= 3)
{
vector<int> temp;
temp.push_back(start);
temp.push_back(end);
res.push_back(temp);
}
flag = S[i];
start = i;
}
}
if(end - start + 1 >= 3)
{
vector<int> temp;
temp.push_back(start);
temp.push_back(end);
res.push_back(temp);
}
return res;
}
};

Leetcode830.Positions of Large Groups较大分组的位置的更多相关文章

  1. 830. Positions of Large Groups

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

  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. 830. Positions of Large Groups - LeetCode

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

  4. 【Leetcode_easy】830. Positions of Large Groups

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

  5. Positions of Large Groups

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

  6. Java实现 LeetCode 830 较大分组的位置(暴力模拟)

    830. 较大分组的位置 在一个由小写字母构成的字符串 S 中,包含由一些连续的相同字符所构成的分组. 例如,在字符串 S = "abbxxxxzyy" 中,就含有 "a ...

  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. C#LeetCode刷题之#830-较大分组的位置(Positions of Large Groups)

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

  9. [LeetCode] Positions of Large Groups 大群组的位置

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

随机推荐

  1. Result结果类型详解

    配置Result 在 struts.xml 文件中,<result> 元素用于配置 Result 逻辑视图与物理视图之间的映射关系,它有两个可选属性 name 和 type.其中,name ...

  2. PHP基于openssl实现的非对称加密操作

    使用非对称加密主要是借助openssl的公钥和私钥,用公钥加密私钥解密,或者私钥加密公钥解密. 1.安装openssl和php的openssl扩展 2.生成私钥:openssl genrsa 用于生成 ...

  3. Simple implementation and results of genetic algorithm.

    This experiment was done for the final assignment of my Professional English class. This part has be ...

  4. Laravel Carbon获取 某个时间后N个月的时间

    $time = "2020-11-20 00:00:00"; $res = (new Carbon)->setTimeFromTimeString($time)->ad ...

  5. 解决分页浏览后搜索无数据的问题(VUE+element-ui)

    开发过程中发现了:浏览到第二页后.对数据进行查询时,后台返回的数据是空.原因是:当前页码为第二页.所以向后台发送请求的pageNumber=2,当pageNumber=1时.就可以查询到数据了. 所以 ...

  6. JavaScript 实例、构造函数、原型对象关系图

    详细介绍:深入理解javascript原型和闭包(5)——instanceof 图片来源:https://www.ibm.com/developerworks/cn/web/1306_jiangjj_ ...

  7. 使用Jedis操作Redis-使用Java语言在客户端操作---String类型

    前提:需要引入Jedis的jar包. /** * 我的redis在Linux虚拟机Centos7中,192.168.222.129是我虚拟机的ip地址. */ private static Jedis ...

  8. 定时任务 $ ls /etc/cron* + cat$ for user in $(cat /etc/passwd | cut -f1 -d:); do crontab -l -u $user; done

    是否有某个定时任务运行过于频繁? 是否有些用户提交了隐藏的定时任务? 在出现故障的时候,是否正好有某个备份任务在执行?

  9. Linux环境变量的种类

    按环境变量的生存周期来划分,Linux的环境变量可分为两类: 1永久的:需要修改配置文件,变量永久生效. 2临时的:使用export命令行声明即可,变量在关闭shell时失效.

  10. ubuntu和win10设置双显示器

    ubuntu:最右上角那个图标,点开找到系统设置,系统设置中找到“显示”中,在其中可以调节双屏显示或者只显示一个屏,图等会补... win10:现在是ubuntu系统所以操作忘记了写不出来,等下换系统 ...