1 Star 0 Fork 0

恐咖兵糖 / www.ftls.xyz

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
2021-10-09-lr352.md 3.46 KB
一键复制 编辑 原始数据 按行查看 历史
恐咖兵糖 提交于 2023-08-13 12:39 . pref: 规范命令 & 整理位置
title: "[LeetCode] 352. 将数据流变为多个不相交区间"
subtitle: ""
slug: "lr352"
date: 2021-10-09T13:30:41+08:00
lastmod: 2021-10-09T13:30:41+08:00
draft: false
description: ""

tags: ["Leetcode-Rust"]
categories: ["Leetcode-Rust"]

featuredImage: ""
featuredImagePreview: ""

hiddenFromHomePage: true

lightgallery: false

题目

给你一个由非负整数 a1, a2, ..., an 组成的数据流输入,请你将到目前为止看到的数字总结为不相交的区间列表。

实现 SummaryRanges 类:

SummaryRanges() 使用一个空数据流初始化对象。 void addNum(int val) 向数据流中加入整数 val 。 int[][] getIntervals() 以不相交区间 [starti, endi] 的列表形式返回对数据流中整数的总结。  

示例:

输入:
["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
[[], [1], [], [3], [], [7], [], [2], [], [6], []]
输出:
[null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]]

解释:
SummaryRanges summaryRanges = new SummaryRanges();
summaryRanges.addNum(1);      // arr = [1]
summaryRanges.getIntervals(); // 返回 [[1, 1]]
summaryRanges.addNum(3);      // arr = [1, 3]
summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3]]
summaryRanges.addNum(7);      // arr = [1, 3, 7]
summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3], [7, 7]]
summaryRanges.addNum(2);      // arr = [1, 2, 3, 7]
summaryRanges.getIntervals(); // 返回 [[1, 3], [7, 7]]
summaryRanges.addNum(6);      // arr = [1, 2, 3, 6, 7]
summaryRanges.getIntervals(); // 返回 [[1, 3], [6, 7]]

提示

0 <= val <= 104 最多调用 addNum 和 getIntervals 方法 3 * 104 次

进阶:如果存在大量合并,并且与数据流的大小相比,不相交区间的数量很小,该怎么办?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/data-stream-as-disjoint-intervals
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

struct SummaryRanges {
    values: Vec<i32>,
}


/**
 * `&self` means the method takes an immutable reference.
 * If you need a mutable reference, change it to `&mut self` instead.
 */
impl SummaryRanges {

    fn new() -> Self {
        SummaryRanges { values: Vec::new() }
    }
    
    fn add_num(&mut self, val: i32) {
        self.values.push(val);
        self.values.sort();
    }
    
    fn get_intervals(&self) -> Vec<Vec<i32>> {
        let mut vec_main:Vec<Vec<i32>> = Vec::new();
        let mut start= 0;
        let mut end = 0;
        loop {
            if self.values.len()==end+1 {
                  vec_main.push(vec![self.values[start],self.values[end]]);
                break;
            }
            if self.values[end]+1==self.values[end+1]||self.values[end]==self.values[end+1] {
                end+=1;
            }
            else {
                vec_main.push(vec![self.values[start],self.values[end]]);
                start = end+1;
                end+=1;
                }
            }
            vec_main
    }
}

/**
 * Your SummaryRanges object will be instantiated and called as such:
 * let obj = SummaryRanges::new();
 * obj.add_num(val);
 * let ret_2: Vec<Vec<i32>> = obj.get_intervals();
 */
1
https://gitee.com/kkbt/www.ftls.xyz.git
git@gitee.com:kkbt/www.ftls.xyz.git
kkbt
www.ftls.xyz
www.ftls.xyz
master

搜索帮助