Algorithms—57.Insert Interval

原创 2015年07月09日 19:15:06

思路:题目不难,就是要考虑好各种情况。

/**
 * 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<Interval> insert(List<Interval> intervals, Interval newInterval) {
		int s = newInterval.start;
		int e = newInterval.end;
		if (intervals.size()==0) {
			intervals.add(newInterval);
			return intervals;
		}
		List<Interval> answerList = new ArrayList<Interval>();
		int ns=0;
		int ne=0;
		boolean beginFlag=true;
		boolean endFlag=true;
		for (int i = 0; i < intervals.size(); i++) {
			if (endFlag&&beginFlag) {
				if (intervals.get(i).start>=s) {
					ns=s;
					beginFlag=false;
				}else if (intervals.get(i).end>=s) {
					if (intervals.get(i).end>=e) {
						endFlag=false;
					}else {
						ns=intervals.get(i).start;
						beginFlag=false;
					}
				}
			}
			if (endFlag&&!beginFlag) {
				if (intervals.get(i).start>e) {
					ne=e;
					answerList.add(new Interval(ns,ne));
					endFlag=false;
					beginFlag=true;
				}else if(intervals.get(i).start==e){
					ne=intervals.get(i).end;
					answerList.add(new Interval(ns,ne));
					endFlag=false;
					beginFlag=true;
					continue;
				}else if(intervals.get(i).end>=e){
					ne=intervals.get(i).end;
					answerList.add(new Interval(ns,ne));
					endFlag=false;
					beginFlag=true;
					continue;
				}else if(i==intervals.size()-1){
					ne=e;
					answerList.add(new Interval(ns,ne));
					return answerList;
				}
			}
			if (beginFlag) {
				answerList.add(intervals.get(i));
			}
		}
		if (endFlag) {
			answerList.add(newInterval);
		}
		return answerList;
	}
}


耗时:460ms,中下游


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

LeetCode(57) Insert Interval

题目Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if neces...
  • fly_yr
  • fly_yr
  • 2015年09月14日 19:24
  • 991

【leetcode】Array——Insert Interval(57)

题目:Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nece...

LeetCode_57---Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa...

[leetcode] 57.Insert Interval

题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if ne...

Leetcode-Array-57(insert Interval)

当然,leetcode拥有良好的函数处理平台,但有时候我想知道如何编写出来一个完整的程序,现在我给出一种编写方法吧,请各位大神多多指教...

leetcode题解-57. Insert Interval

题目:Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nece...

lkeetcode 57. Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa...

LeetCode 57. Insert Interval/56. Merge Intervals

1. 题目要求 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge ...

120. Triangle\57. Insert Interval\84. Largest Rectangle in Histogram*

Triangle 题目描述 代码实现 Insert Interval 题目描述 代码实现 Largest Rectangle in Histogram 题目描述 代码描述120. Triangle题目...

Leetcode 57 Insert Interval

一,问题描述:1,给定一个区间集合,这些区间有些是重合的。另外给一个区间,然后把这些区间,进行合并有重叠的区间2,例如: 给定一个区间集合: [1 3] [6 9] 再给一个区间[2 5...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Algorithms—57.Insert Interval
举报原因:
原因补充:

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