C#LeetCode刷题之#830-较大分组的位置(Positions of Large Groups)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3750 访问。
在一个由小写字母构成的字符串 S 中,包含由一些连续的相同字符所构成的分组。
例如,在字符串 S = "abbxxxxzyy" 中,就含有 "a", "bb", "xxxx", "z" 和 "yy" 这样的一些分组。
我们称所有包含大于或等于三个连续字符的分组为较大分组。找到每一个较大分组的起始和终止位置。
最终结果按照字典顺序输出。
输入: "abbxxxxzzy"
输出: [[3,6]]
解释: "xxxx" 是一个起始于 3 且终止于 6 的较大分组。
输入: "abc"
输出: []
解释: "a","b" 和 "c" 均不是符合要求的较大分组。
输入: "abcdddeeeeaabbbcd"
输出: [[3,5],[6,9],[12,14]]
说明: 1 <= S.length <= 1000
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.
Input: "abbxxxxzzy"
Output: [[3,6]]
Explanation: "xxxx" is the single large group with starting 3 and ending positions 6.
Input: "abc"
Output: []
Explanation: We have "a","b" and "c" but no large group.
Input: "abcdddeeeeaabbbcd"
Output: [[3,5],[6,9],[12,14]]
Note: 1 <= S.length <= 1000
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3750 访问。
public class Program {
public static void Main(string[] args) {
string S = string.Empty;
S = "abbxxxxzzy";
var res = LargeGroupPositions(S);
ShowArray(res);
S = "abcdddeeeeaabbbcd";
res = LargeGroupPositions2(S);
ShowArray(res);
Console.ReadKey();
}
private static void ShowArray(IList<IList<int>> array) {
foreach(var list in array) {
foreach(var index in list) {
Console.Write($"{index} ");
}
}
Console.WriteLine();
}
private static IList<IList<int>> LargeGroupPositions(string S) {
var result = new List<IList<int>>();
var last = '\0';
var startIndex = -1;
var endIndex = -1;
for(var i = 0; i < S.Length; i++) {
if(S[i] != last || i == S.Length - 1) {
endIndex = i - 1;
if(i == S.Length - 1 && S[i] == last) endIndex = i;
if(endIndex - startIndex + 1 >= 3) {
var item = new List<int>();
item.Add(startIndex);
item.Add(endIndex);
result.Add(item);
}
startIndex = i;
}
last = S[i];
}
return result;
}
private static IList<IList<int>> LargeGroupPositions2(string S) {
var result = new List<IList<int>>();
for(var i = 0; i < S.Length; i++) {
var next = i + 1;
var dic = new Dictionary<int, int>();
while(next < S.Length && S[next] == S[i]) {
if(next - i >= 2) {
dic[i] = next;
}
next++;
}
if(dic.TryGetValue(i, out int value)) {
result.Add(new int[] { i, value });
i = next - 1;
//循环内部更改循环计数是一种不好的做法,切记切记
//如果不是必需,请勿模仿此做法
}
}
return result;
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3750 访问。
3 6
3 5 6 9 12 14
分析:
比较简单的一道题,主要是处理好边界,需要细心调试,LargeGroupPositions的时间复杂度为: ;LargeGroupPositions2的时间复杂度也为: ,虽然有内部while循环,但是计数被后移了,数组实际是还是被扫描了一次。
另外,循环内部更改循环计数是一种不好的做法,如果不是必需,请勿模仿此做法,切记切记!
最新文章
- [spring源码学习]八、IOC源码-messageSource
- Linux不使用useradd创建用户
- Ionic2 Tutorial
- oracle数据库如何创建角色并对角色授予权限
- 简明 Vim 练级攻略(转)
- linux里的进程简介
- JAVA_build_ant_Tstamp
- [Swust OJ 247]--皇帝的新衣(组合数+Lucas定理)
- java获取日期之间的差异
- C++ 拷贝构造函数、拷贝赋值运算符、析构函数
- Java多线程中的单例模式
- Hibernate的入门:
- 019_Mac实用的图像备份工具
- E. Thematic Contests 二分,离散化
- 解决:Windows安装Composer及全局配置时提示部分.dll结尾的php扩展文件找不到指定的模板
- mysql date
- python 美化打印json数据
- C++ Templates 关于程序库的概念和通用工具
- 数据分箱:等频分箱,等距分箱,卡方分箱,计算WOE、IV
- 库的操作&;表的操作
热门文章
- 谈谈IT圈的门槛与学历的关系以及如何避免青春饭?
- Ethical Hacking - Web Penetration Testing(9)
- T133305 57级返校测试重测-T1-数字配对
- vue : 使用stylus less (包括sublime插件支持)
- shell脚本sql赋值
- Monster Audio 使用教程(一)入门教程 + 常见问题
- json:server 本地搭建
- TSGCTF-web Beginner&#39;s Web (js内置方法__defineSetter__)
- 企业权限管理(SSM整合)(总结)
- PHP array_push() 函数