LeetCode 352 将数据流变为多个不相交区间

在这里插入图片描述

class SummaryRanges {

    List<List<Integer>> list;
    
    public SummaryRanges() {
        list = new ArrayList<List<Integer>>();
    }
    
    public void addNum(int val) {
        if(list.isEmpty()) {
		List<Integer> tempList = new ArrayList<>();
            tempList.add(val);
            list.add(tempList);
        }else {
            int size = list.size();
            List<Integer> lastList = list.get(size-1);
            List<Integer> firstList = list.get(0);
            Integer min = firstList.get(0);
            Integer max = lastList.get(lastList.size()-1);
			if(val < min) {
                if(val+1 == min) {
                    firstList.add(0, val);
                }else {
                    List<Integer> tempList = new ArrayList<>();
                    tempList.add(val);
                    list.add(0, tempList);
                }
            }else if(val > max){
			if(val-1 == max) {
                    lastList.add(val);
                }else {
                    List<Integer> tempList = new ArrayList<>();
                    tempList.add(val);
                    list.add(tempList);
                }
            }else {
                for(int i = 0; i < list.size(); i++) {
				List<Integer> tempList = list.get(i);
                    int start = tempList.get(0);
                    int end = tempList.get(tempList.size()-1);
                    if(val >= start && val <= end) {
                        break;
                    }
                    if(val > end) {
                        List<Integer> nextList = list.get(i+1);
                        Integer next = nextList.get(0);
                        if(val == end+1) {
						if(val+1 == next) {
                                tempList.add(nextList.get(nextList.size()-1));
                                list.remove(i+1);
                                break;
                            }else {
                                tempList.add(val);
                                break;
                            }
                        }else {
                            if(val < next) {
                                if(val+1 == next) {
                                    nextList.add(0, val);
                                    break;
                                }else {
                                    List<Integer> addList = new ArrayList<>();
                                    addList.add(val);
                                    list.add(i+1, addList);
                                    break; 
                                }
                            }
                        }
                    }else if(val < start) {
					List<Integer> beforeList = list.get(i-1);
                        if(val == start-1) {
                            if(val-1 == beforeList.get(beforeList.size()-1)) {
                                tempList.add(beforeList.get(0));
                                list.remove(i-1);
                                break;
                            }else {
                                tempList.add(0, val);
                                break;
                            }
                        }
					}
                }
            }
        }
    }
	
	public int[][] getIntervals() {
        int length = list.size();
        int[][] resArr = new int[length][2];
        for(int i = 0; i < list.size(); i++) {
            resArr[i][0] = list.get(i).get(0);
            List<Integer> temp = list.get(i);
            resArr[i][1] = temp.get(temp.size()-1);
        }
        return resArr;
    }
}

结果
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值