首页 > 其他分享 >leetcode-830-easy

leetcode-830-easy

时间:2023-04-14 22:37:01浏览次数:42  
标签:begin end groups 830 large add easy group leetcode

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".

A group is identified by an interval [start, end], where start and end denote the start and end indices (inclusive) of the group. In the above example, "xxxx" has the interval [3,6].

A group is considered large if it has 3 or more characters.

Return the intervals of every large group sorted in increasing order by start index.

Example 1:

Input: s = "abbxxxxzzy"
Output: [[3,6]]
Explanation: "xxxx" is the only large group with start index 3 and end index 6.
Example 2:

Input: s = "abc"
Output: []
Explanation: We have groups "a", "b", and "c", none of which are large groups.
Example 3:

Input: s = "abcdddeeeeaabbbcd"
Output: [[3,5],[6,9],[12,14]]
Explanation: The large groups are "ddd", "eeee", and "bbb".
Constraints:

1 <= s.length <= 1000
s contains lowercase English letters only.

思路一:直接遍历,在遍历的过程中统计前后下标的距离是否大于等于 2

    public List<List<Integer>> largeGroupPositions(String s) {
        char[] chars = s.toCharArray();

        int begin = 0;
        int end = 0;
        List<List<Integer>> result = new ArrayList<>();
        for (int i = 1; i < chars.length; i++) {

            if (chars[i] == chars[i - 1]) {
                end = i;
            } else {
                List<Integer> t = new ArrayList<>();
                t.add(begin);
                t.add(end);

                if (end - begin >= 2) {
                    result.add(t);
                }
                begin = i;
                end = i;

            }
        }
        if (end - begin >= 2) {
            List<Integer> t = new ArrayList<>();
            t.add(begin);
            t.add(end);
            result.add(t);
        }

        return result;
    }

思路二:看了一下题解,发现在字符串后面追加一个字符,可以省去最后一个串的判断

标签:begin,end,groups,830,large,add,easy,group,leetcode
From: https://www.cnblogs.com/iyiluo/p/17320132.html

相关文章

  • leetcode-812-easy
    LargestTriangleAreaGivenanarrayofpointsontheX-Yplanepointswherepoints[i]=[xi,yi],returntheareaofthelargesttrianglethatcanbeformedbyanythreedifferentpoints.Answerswithin10-5oftheactualanswerwillbeaccepted.Exampl......
  • leetcode-944-easy
    DeleteColumnsToMakeSortedYouaregivenanarrayofnstringsstrs,allofthesamelength.Thestringscanbearrangedsuchthatthereisoneoneachline,makingagrid.Forexample,strs=["abc","bce","cae"]canb......
  • 代码随想录算法训练营Day01 | LeetCode704 二分查找、Leetcode27 移除元素
    今日学习的视频和文章代码随想录数组基础复习基础知识代码随想录二分查找代码随想录移除元素LeetCode704二分查找题目链接:704.二分查找-力扣(Leetcode)以前学二分查找的时候,真的一直搞不清楚怎么操作左边界和有边界,以及循环的终止条件是什么,总是自己慢慢调试出来,......
  • LeetCode 周赛 340,质数 / 前缀和 / 极大化最小值 / 最短路 / 平衡二叉树
    本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]提问。大家好,我是小彭。上周跟大家讲到小彭文章风格的问题,和一些朋友聊过以后,至少在算法题解方面确定了小彭的风格。虽然竞赛算法题的文章受众非常小,但却有很多像我一样的初学者,他们有兴趣参加但容易被题目难......
  • 视频直播点播EasyDSS迁移至新服务器,启动正常但无法访问是什么原因?
    EasyDSS能实现视频流媒体的上传、转码、存储、录像、推拉流、直播、点播等功能,具备超低延迟、超高画质、超大并发访问量等特点,可应用在多样化的场景中,如:在线课堂、教育直播、校园活动直播、企业培训、游戏直播等。平台支持HTTP、HLS、RTMP等播出协议,并且兼容多终端,如:Windows、Andro......
  • 在EasyCVR中点击电子地图,出现快照不消失情况是什么原因?
    EasyCVR视频融合平台基于云边端一体化架构,部署轻快、功能灵活,平台可支持多协议、多类型设备接入,包括:国标GB28181、RTMP、RTSP/Onvif、海康Ehome、海康SDK、大华SDK、宇视SDK等(具体见下图)。在视频能力上,可实现视频直播、录像、回放、检索、云存储、告警上报、语音对讲、电子地图、集......
  • LeetCode 108.将有序数组转换成二叉搜索树
    1.题目:给你一个整数数组nums,其中元素已经按升序排列,请你将其转换为一棵高度平衡二叉搜索树。高度平衡二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过1」的二叉树。示例1:输入:nums=[-10,-3,0,5,9]输出:[0,-3,9,-10,null,5]解释:[0,-10,5,null,-3,null,......
  • [LeetCode] 1440. Jump Game V 跳跃游戏之五
    Givenanarrayof integers arr andaninteger d.Inonestepyoucanjumpfromindex i toindex:i+x where: i+x<arr.length and 0< x<=d.i-x where: i-x>=0 and 0< x<=d.Inaddition,youcanonlyjumpfromindex i toi......
  • 【前缀和】LeetCode 1031. 两个非重叠子数组的最大和
    题目链接1031.两个非重叠子数组的最大和思路代码classSolution{publicintmaxSumTwoNoOverlap(int[]nums,intfirstLen,intsecondLen){//求一个前缀和for(inti=1;i<nums.length;++i){nums[i]+=nums[i-1];}......
  • [oeasy]python0133_[趣味拓展]颜文字_流石兄弟_表情文字_2ch_kaomoji
    颜文字回忆上次内容上次我们了解unicode里面有各种字体甚至还有emoji emoji本质上也是文字按照unicode的方式编码存储时按照utf-8的方式编码显示时按照系统定义的方式进行显示 还有什么好玩的亚文化吗?......