题目描述:

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

要完成的函数:

vector<vector<int>> largeGroupPositions(string S)

说明:

1、给定一个字符串S,如果一个字符连续出现三次及三次以上,那么它就是一个“大组合”,要求找出所有“大组合”的起始位置和结束位置,最终以vector<vector<int>>的形式返回。

2、明白题意,这又是一道简单题。

直接贴上代码(附详解),如下:

    vector<vector<int>> largeGroupPositions(string S)
{
int s1=S.size(),i=0,j;
vector<vector<int>>res;//最后返回的vector
vector<int>res1;//子vector
while(i<s1-2)
{
if(S[i]==S[i+1]&&S[i]==S[i+2])//如果满足条件
{
res1.clear();//清空res1
res1.push_back(i);//插入起始位置到res1
j=i+3;
while(j<s1)//找到不等于S[i]的字符位置
{
if(S[i]==S[j])
j++;
else
break;
}
res1.push_back(j-1);//插入结束位置到res1
res.push_back(res1);//res1插入到res里面
i=j;//更新i的值
}
else
i++;
}
return res;
}

上述代码实测13ms,因为服务器接收到的cpp submissions有限,所以没有打败的百分比。

leetcode-830-Positions of Large Groups的更多相关文章

  1. 830. Positions of Large Groups - LeetCode

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

  2. 【Leetcode_easy】830. Positions of Large Groups

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

  3. 830. Positions of Large Groups@python

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

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

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

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

  6. [LeetCode] 830. Positions of Large Groups_Easy tag: Two Pointers

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

  7. 830. Positions of Large Groups

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

  8. Positions of Large Groups

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

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

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

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

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

随机推荐

  1. SQL SERVER性能优化综述

    SQL SERVER性能优化综述 一个系统的性能的提高,不单单是试运行或者维护阶段的性能调优的任务,也不单单是开发阶段的事情,而是在整个软件生命周期都需要注意,进行有效工作才能达到的.所以我希望按照软 ...

  2. AJAX(XMLHttpRequest)进行跨域请求方法详解

    AJAX(XMLHttpRequest)进行跨域请求方法详解(三) 2010年01月11日 08:48:00 阅读数:24213 注意:以下代码请在Firefox 3.5.Chrome 3.0.Saf ...

  3. sqlserver 时间函数用法

    1.DATEADD(datepart,number,date) 现在,我们希望向 "OrderDate" 添加 2 天,这样就可以找到付款日期,我们使用如下 SELECT 语句: ...

  4. 初学者教程之命名空间,范围解析及LEDB规则

    2014年5月12日 Sebastian Raschka编写 这是一篇关于采用LEGB规则实现Python变量命名空间及范围解析的简短教程.下面章节将会提供简短的可以说明问题的示例代码块来简要阐述问题 ...

  5. 最近公共祖先 LCA Tarjan算法

    来自:http://www.cnblogs.com/ylfdrib/archive/2010/11/03/1867901.html 对于一棵有根树,就会有父亲结点,祖先结点,当然最近公共祖先就是这两个 ...

  6. http post 方法传递参数的2种方式

       1.StringEntity try{ HttpPost httpPost = new HttpPost(url); //param参数,可以为param="key1=value1&a ...

  7. InteliJ中文乱码;IDE快捷键使用

    启动服务器的时候出现如图 解决方法: 对服务器的位置进行编辑 增加如图的信息 -Dfile.encoding=UTF-8

  8. SQLServer学习-- Microsoft SQL Server 2008 Management Studio Express

    Microsoft SQL Server 2008 Management Studio Express is a free, integrated environment for accessing, ...

  9. 引用数据类型(类)和ArrayList

    引用数据类型(类) 类的类型为两种: 第一种,Java为我们提供好的类,如Scanner类,Scanner类等,这些已存在的类中包含了很多的方法与属性,可供我们使用. 第二种,我们自己创建的类,按照类 ...

  10. JPA和Hibernate的相关使用技巧

    介绍 尽管有SQL标准,但每个关系数据库终将是唯一的,因此你需要调整数据访问层,以便充分利用在使用中的关系数据库. 在本文中,我们将介绍在使用带有JPA和Hibernate的MySQL时,为了提高性能 ...