问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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循环,但是计数被后移了,数组实际是还是被扫描了一次。

另外,循环内部更改循环计数是一种不好的做法,如果不是必需,请勿模仿此做法,切记切记!

C#LeetCode刷题之#830-较大分组的位置(Positions of Large Groups)的更多相关文章

  1. [Swift]LeetCode830. 较大分组的位置 | Positions of Large Groups

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

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

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

  3. leetcode刷题-49字母异位词分组

    题目 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 思路 由于异位词由相同字母构成,所以可以用一个顺序的字符串作为这些字母异位词的标志,由此可以想到字典的方法 ...

  4. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  5. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  6. LeetCode刷题总结之双指针法

    Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链 ...

  7. LeetCode刷题总结-数组篇(上)

    数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...

  8. LeetCode刷题总结-数组篇(下)

    本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...

  9. LeetCode刷题总结-树篇(上)

          引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...

  10. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

随机推荐

  1. nginx一个端口配置多个不同服务映射

    upstream tomcat_server{ server 127.0.0.1:8087; server 192.168.149.117:8088; } server { listen 8088; ...

  2. Ethical Hacking - GAINING ACCESS(19)

    Client-Side Attacks - Social Engineering Tool: The FAT RAT Just like Veil, it generates Undetectable ...

  3. Python Ethical Hacking - BACKDOORS(7)

    Handling Errors: If the client or server crashes, the connection will be lost. Backdoor crashes if: ...

  4. 手把手撸套框架-ORM框架的选择

    目录 一,为什么选择SqlSugar? 在.net core ORM框架中,能选择的方案其实有很多,包括以下方案: 1,EF-Core 2,Dapper 3,FreeSql 4,SqlSugar 为什 ...

  5. iview实战 : 树形组件自定义

    Tree树形组件是 iview 中相对复杂的一个组件. 自定义节点内容 使用强大的 Render 函数可以自定义节点显示内容和交互,比如添加图标,按钮等. ——官方文档 但官方的 example 只有 ...

  6. Elasticsearch源码解析:环境搭建

    在之前学习Redis的过程中,我就是一边通过文档/视频学习,一边阅读源码.我发现两种方法可以相辅相成,互相补充.阅读文档可以帮助我们快速了解某个功能,阅读源码有助于我们更深入的理解这一功能的实现及思想 ...

  7. DataTable 转 JSON,XML转JSON

    今天总结一下关于DataTable,XML转JSON的方法: 首先需要引入命名空间: using Newtonsoft.Json 1 public string DataTableToJsonWith ...

  8. vue学习(十九) 生命周期 了解

    生命周期:从vue实例创建.运行.到销毁期间,总是伴随着各种各样的事件,这些事件统称为生命周期 生命周期钩子:就是生命周期事件的别名而已 生命周期钩子==生命周期函数==生命周期事件 生命周期函数分类 ...

  9. 数字麦克风PDM信号采集与STM32 I2S接口应用(四)--单片机源码

    本文是数字麦克风笔记文章的单片机程序.一些朋友私信我,调试出问题. 我就把源码贴出来吧,可能主要问题是DMA的配置. 尤其双DMA时候,需要手动启动I2S的接收DMA,HAL库没有这个接口,不看dat ...

  10. SQL语法分类

    数据查询 语法格式 : select [ , ...] from table_reference [ , ...] 去重复值 distinct关键字 , 从select结果集中删除所有重复的行,使结果 ...