LeetCode—352. Data Stream as Disjoint Intervals

原创 2016年06月01日 15:38:05

思路:设定3个map,finsh(装载已add过的数据),before(add的val-1),end(add的val+1),list中存储答案

finish中get(val)不为null,则处理,为null则不处理

before和end同时不为null,取出前后Interval,list中删掉前Interval,后Interval的start=前Interval的start。删除end的数据,替换beofe的数据

before同为空,则单独新增

任意一个为空另一个不为空,修改对应的值


GitHub地址:https://github.com/corpsepiges/leetcode

有Java版本和Python版本,求star。


<span style="font-size:14px;">/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class SummaryRanges {
	Map<Integer, Interval> before ;
	Map<Integer,Interval> end;
	Map<Integer,Integer> finish;
	List<Interval> list;
    /** Initialize your data structure here. */
    public SummaryRanges() {
    	before=new HashMap<Integer, Interval>();
    	end=new HashMap<Integer, Interval>();
    	finish=new HashMap<Integer,Integer>();
        list=new ArrayList<>();
    }
    
    public void addNum(int val) {
    	if (finish.get(val)==null) {
    		if (before.get(val)==null&&end.get(val)==null) {
    			Interval iv=new Interval(val,val);
    			before.put(val-1, iv);
    			end.put(val+1, iv);
    			if (list.size()==0) {
    				list.add(iv);
    			}else if (list.get(0).start>val) {
    				list.add(0, iv);
    			}else{
    				for (int i = list.size()-1; i>=0; i--) {
    					if (val>list.get(i).end) {
    						list.add(i+1,iv);
    						break;
    					}
    				}
    			}
    		}else if(before.get(val)==null){
    			end.get(val).end=val;
    			end.put(val+1, end.get(val));
    			end.remove(val);
    		}else if(end.get(val)==null){
    			before.get(val).start=val;
    			before.put(val-1, before.get(val));
    			before.remove(val);
    		}else{
    			Interval b=end.get(val);
    			Interval e=before.get(val);
    			e.start=b.start;
    			end.remove(val);
    			before.remove(val);
    			before.put(b.start-1, e);
    			list.remove(b);
    		}
    		finish.put(val, 1);
		}
    }
    
    public List<Interval> getIntervals() {
        return list;
    }
}

/**
 * Your SummaryRanges object will be instantiated and called as such:
 * SummaryRanges obj = new SummaryRanges();
 * obj.addNum(val);
 * List<Interval> param_2 = obj.getIntervals();
 */</span>


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode 352. Data Stream as Disjoint Intervals(数据流区间)

原题网址:https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input...
  • jmspan
  • jmspan
  • 2016年06月16日 01:11
  • 938

[leetcode] 352. Data Stream as Disjoint Intervals 解题报告

题目链接: https://leetcode.com/problems/data-stream-as-disjoint-intervals/ Given a data stream input o...

352. Data Stream as Disjoint Intervals

352. Data Stream as Disjoint Intervals

LeetCode295——Find Median from Data Stream

LeetCode295——Find Median from Data Stream 前提是Ordered List。 但是插入即addNum操作可能是无序的。 尝试过对序列排序再返回的办法结果是...

Leetcode 295. Find Median from Data Stream (Hard) (cpp)

Leetcode 295. Find Median from Data Stream (Hard) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年12月14日 00:54
  • 203

leetcode -- Find Median from Data Stream -- 设计题重点

https://leetcode.com/problems/find-median-from-data-stream/利用minHeap以及maxHeap。把一个ordered num list, 分...
  • xyqzki
  • xyqzki
  • 2015年12月25日 10:58
  • 310

[leetcode] 295. Find Median from Data Stream

题目:Median is the middle value in an ordered integer list. If the size of the list is even, there is ...

leetcode No295. Find Median from Data Stream

QuestionMedian is the middle value in an ordered integer list. If the size of the list is even, ther...

[leetcode] 295. Find Median from Data Stream

Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

leetcode 295 : Find Median from Data Stream

1、原题如下: Median is the middle value in an ordered integer list. If the size of the list is even, the...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode—352. Data Stream as Disjoint Intervals
举报原因:
原因补充:

(最多只允许输入30个字)