• leetcode 830 较大分组的位置


    package com.example.lettcode.dailyexercises;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @Class LargeGroupPositions
     * @Description 830 较大分组的位置
     * 在一个由小写字母构成的字符串 s 中,包含由一些连续的相同字符所构成的分组。
     * 例如,在字符串 s = "abbxxxxzyy" 中,就含有 "a", "bb", "xxxx", "z" 和 "yy" 这样的一些分组。
     * 分组可以用区间 [start, end] 表示,其中 start 和 end 分别表示该分组的起始和终止位置的下标。
     * 上例中的 "xxxx" 分组用区间表示为 [3,6] 。
     * 我们称所有包含大于或等于三个连续字符的分组为 较大分组 。
     * <p>
     * 找到每一个 较大分组 的区间,按起始位置下标递增顺序排序后,返回结果。
     * <p>
     * 示例 1:
     * 输入:s = "abbxxxxzzy"
     * 输出:[[3,6]]
     * 解释:"xxxx" 是一个起始于 3 且终止于 6 的较大分组。
     * <p>
     * 示例 2:
     * 输入:s = "abc"
     * 输出:[]
     * 解释:"a","b" 和 "c" 均不是符合要求的较大分组。
     * <p>
     * 示例 3:
     * 输入:s = "abcdddeeeeaabbbcd"
     * 输出:[[3,5],[6,9],[12,14]]
     * 解释:较大分组为 "ddd", "eeee" 和 "bbb"
     * <p>
     * 示例 4:
     * 输入:s = "aba"
     * 输出:[]
     * <p>
     * 提示:
     * 1 <= s.length <= 1000
     * s 仅含小写英文字母
     * @Author
     * @Date 2021/1/5
     **/
    public class LargeGroupPositions {
        public static List<List<Integer>> largeGroupPositions(String s) {
            if (s == null || s.length() < 3) return new ArrayList<>();
    
            List<List<Integer>> resLists = new ArrayList<>();
            int start = 0, end = 1;
            char ch = s.charAt(start);
            for (int i = 1; i < s.length(); i++) {
                if (s.charAt(i) == ch) {
                    end = i + 1;
                } else {
                    if (end - start >= 3) {
                        List<Integer> integers = new ArrayList<>();
                        integers.add(start);
                        integers.add(end - 1);
                        resLists.add(integers);
                    }
                    start = i;
                    end = i + 1;
                    ch = s.charAt(start);
                }
            }
            // 最后一个较大分组
            if (end - start >= 3) {
                List<Integer> integers = new ArrayList<>();
                integers.add(start);
                integers.add(end-1);
                resLists.add(integers);
            }
            return resLists;
        }
    }
    
    // 测试用例
    public static void main(String[] args) {
    	String s = "abbxxxxzzy";
    	List<List<Integer>> ansLists = largeGroupPositions(s);
    	System.out.print("LargeGroupPositions demo01 result : [");
    	for (List<Integer> integers : ansLists) {
    		System.out.print("[" + integers.get(0) + "," + integers.get(1) + "]");
    	}
    	System.out.println("]");
    
    	s = "abc";
    	ansLists = largeGroupPositions(s);
    	System.out.print("LargeGroupPositions demo02 result : [");
    	for (List<Integer> integers : ansLists) {
    		System.out.print("[" + integers.get(0) + "," + integers.get(1) + "]");
    	}
    	System.out.println("]");
    
    	s = "abcdddeeeeaabbbcd";
    	ansLists = largeGroupPositions(s);
    	System.out.print("LargeGroupPositions demo03 result : [");
    	for (List<Integer> integers : ansLists) {
    		System.out.print("[" + integers.get(0) + "," + integers.get(1) + "]");
    	}
    	System.out.println("]");
    
    	s = "aba";
    	ansLists = largeGroupPositions(s);
    	System.out.print("LargeGroupPositions demo04 result : [");
    	for (List<Integer> integers : ansLists) {
    		System.out.print("[" + integers.get(0) + "," + integers.get(1) + "]");
    	}
    	System.out.println("]");
    
    	s = "abaaaa";
    	ansLists = largeGroupPositions(s);
    	System.out.print("LargeGroupPositions demo05 result : [");
    	for (List<Integer> integers : ansLists) {
    		System.out.print("[" + integers.get(0) + "," + integers.get(1) + "]");
    	}
    	System.out.println("]");
    
    	s = "aaa";
    	ansLists = largeGroupPositions(s);
    	System.out.print("LargeGroupPositions demo06 result : [");
    	for (List<Integer> integers : ansLists) {
    		System.out.print("[" + integers.get(0) + "," + integers.get(1) + "]");
    	}
    	System.out.println("]");
    }
    
  • 相关阅读:
    oracle无监听解决方案
    存储过程:期报提示(含有数组)
    分库分表?如何做到永不迁移数据和避免热点?
    存储过程期报提示生成
    Controller层@PathVariable使用
    Java系统架构师学习体系图
    Command line is too long. Shorten command line for xxxxxxxxxxxxxxxxxx
    手动部署oceanbase
    OceanBase Docker 体验
    oceanabse执行计划查看
  • 原文地址:https://www.cnblogs.com/fyusac/p/14236102.html
Copyright © 2020-2023  润新知