leetcode-830-Positions of Large Groups
题目描述:
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的更多相关文章
- 830. Positions of Large Groups - LeetCode
Question 830. Positions of Large Groups Solution 题目大意: 字符串按连续相同字符分组,超过3个就返回首字符和尾字符 思路 : 举例abcdddeeee ...
- 【Leetcode_easy】830. Positions of Large Groups
problem 830. Positions of Large Groups solution1: class Solution { public: vector<vector<int&g ...
- 830. Positions of Large Groups@python
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- 【LeetCode】830. Positions of Large Groups 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [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 ...
- [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 ...
- 830. Positions of Large Groups
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- Positions of Large Groups
Positions of Large Groups In a string S of lowercase letters, these letters form consecutive groups ...
- [LeetCode] Positions of Large Groups 大群组的位置
In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...
- C#LeetCode刷题之#830-较大分组的位置(Positions of Large Groups)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3750 访问. 在一个由小写字母构成的字符串 S 中,包含由一些连 ...
随机推荐
- WebPack 从安装到闲置
序言:各种技术在研究过程中常常会出现在实际工作中难以实施的情况,于是就慢慢闲置,但学毕竟还是必须要学学的,就看能用到多少,至少开拓了眼界,谨以此安慰下那些学完又闲置的技术~ 跑题结束,以下开始正式配置 ...
- mybaties association 只返回一个结果问题处理
mybatis xml文件为: <resultMap id="BaseResultMap" type="com.test.SubscribeOrder"& ...
- LoadRunner11学习记录四 -- 集合点
LoadRunner集合点的设置: 我们来想象一个场景,10名运动员参加长跑比赛,出发点同时起跑,他们是并排奔跑的:跑了N圈之后,因为有体能更强的,有体能稍弱的,他们的队形并排变成了前后.几乎一个跑道 ...
- oracle数据库学习记录(持续更新中...)
--------------------------------------------day1------------------------------------------------- 1. ...
- linux环境下搭建osm_web服务器四(对万国语的地名进行翻译和检索):
对万国语的地名进行翻译和检索 经过 前三篇的调试,已经有了一个完整的Map可以浏览,我们痛苦的世界范围数据下载.导入过程也结束了.要提醒一下的是,鉴于网速,不要下载 planetosm.lastest ...
- zigbee组播通信原理
组播: 在zigbee网络里面,把网络节点标记为组的方式来进行通信:发送模块如果发送的组号和网络里标记模块的组号相对应,那么这些模块就可以拿到这些无线数据包. 特点: 1.分组中组的编号有两个字节. ...
- C# 进程Process基本的操作说明
public int CallPhoneExe(string arg) //arg为进程的命令行参数 { WaitHandle[] waits =new WaitHandle[2]; //定义两个Wa ...
- jQuary总结2: jQuery选择器
1 什么是jQuery选择器 获取页面元素,并且把页面元素包装成jQuery对象的方式 2 为什么要学习jQuery选择器 为了更加方便的获取页面上的元素,并且将元素包装起来,使我们编写程序时更加便 ...
- RollingFileAppender
http://logback.qos.ch/manual/appenders.html#RollingFileAppender 1.基于时间的滚动策略 TimeBasedRollingPolicy 就 ...
- 编写高质量代码改善C#程序的157个建议——建议154:不要过度设计,在敏捷中体会重构的乐趣
建议154:不要过度设计,在敏捷中体会重构的乐趣 有时候,我们不得不随时更改软件的设计: 如果项目是针对某个大型机构的,不同级别的软件使用者,会提出不同的需求,或者随着关键岗位人员的更替,需求也会随个 ...