在一个由小写字母构成的字符串 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. IO流16 --- 对象流操作字符串 --- 技术搬运工(尚硅谷)

    序列化 @Test public void test12() throws IOException { ObjectOutputStream oos = new ObjectOutputStream( ...

  2. Flask session到期时间设置 用户登录与登出

    flask版本 1.1.1 最近学习Flask开发,看官方文档产生疑问,就是session有效期的问题,默认貌似是没有有效期的,只有关闭浏览器session才会失效,其实控制session的有效期非常 ...

  3. Vue. 之 刷新当前页面,重载页面数据

    Vue. 之 刷新当前页面,重载页面数据 如下截图,点击左侧不同的数据,右侧根据左侧的KEY动态加载数据.由于右侧是同一个页面,在进行路由跳转后,不会再次刷新数据. 解决方案: 右侧的页面中 scri ...

  4. Matlab 路径函数

    1 fileparts [pathstr,name,ext] = fileparts(filename) 将filename字符串分解成路径,文件名和文件后缀.文件可以不存在,ext中含有前缀dot( ...

  5. storm运行服务器一些错误解决、

    java.lang.RuntimeException: Returned channel was actually not established 重启试试 Java.lang.NoSuchMetho ...

  6. Windows 的 80 端口被 System 进程占用解决方案

    通过 Windows 的资源监视器(win+R:resmon)可以看到 80 端口已经被占用,下图是已经解决好了,没能截图被占用的情况,下面给出解决方案. PS:贴出两个好用的 windows cmd ...

  7. 高斯消元和高斯约旦消元 Gauss(-Jordan) Elimination

    高斯消元法,是线性代数中的一个算法,可用来求解线性方程组,并可以求出矩阵的秩,以及求出可逆方阵的逆矩阵. 在讲算法前先介绍些概念 矩阵的初等变换 矩阵的初等变换又分为矩阵的初等行变换和矩阵的初等列变换 ...

  8. hdu6243

    hdu6243结论题,每个的概率是(n-1)/n,然后乘以总数n,结果就是 n-1 #include<iostream> #include<cstdio> #include&l ...

  9. leetcode 352 & leetcode 239 & leetcode 295 & leetcode 53 & leetcode 209

    lc352 Data Stream as Disjoint Intervals 可以用treemap解 key保存interval的start,value保存interval的end.分别找出当前va ...

  10. 用canvas 画出圆形图片

    /** * 把图片处理成圆形,如果不是正方形就按最小边一半为半径处理 * @param {object} imgObj 图片(img)对象 * @param {number} imgType 设置生成 ...