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的时间复杂度为: O(n) ;LargeGroupPositions2的时间复杂度也为: O(n) ,虽然有内部while循环,但是计数被后移了,数组实际是还是被扫描了一次。

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值