程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> [LeetCode]Merge Intervals

[LeetCode]Merge Intervals

編輯:C++入門知識

[LeetCode]Merge Intervals


Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

參照insert intervals

/**
 * 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 Solution {
    public List merge(List intervals) {
    	List res = new ArrayList<>();
    	for(int i=0;i insert(List intervals, Interval newInterval) {
    	List list = new ArrayList();
    	if(intervals.size()==0){
    		list.add(newInterval);
    		return list;
    	}
		int start = newInterval.start;
		int end = newInterval.end;
		int mstart=-1;
		int mend=-1;
		int nstart = -1;
		int nend = -1;
		for(int i=0;iintervals.get(intervals.size()-1).end){
				list = intervals;
				list.add(newInterval);
			}else if(end




  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved