leetcode 57:Insert Interval

原创 2015年11月18日 20:31:36

题目:

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

You may assume that the intervals were initially sorted according to their start times.

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

思路:

这题思路和Merge Intervals比较像,由于已经排序,所以不需进行排序,然后插入集合,进行合并。

题目不难,但是注意边界条件即可。

时间复杂度:O(n)

实现如下:

class Solution {
public:
	vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
		int size = intervals.size();
		vector<Interval> result;
		if (size == 0)
		{
			result.push_back(newInterval);
			return result;
		}
		int i = 0,low,high;
		while (i<size && intervals[i].end < newInterval.start)
		{
			result.push_back(intervals[i]);
			++i;
		}
		if (i<size && newInterval.end < intervals[i].start)
			result.push_back(newInterval);
		else
		{
			low = i < size ? min(intervals[i].start, newInterval.start) : newInterval.start;
			while (i < size && newInterval.end >= intervals[i].start) ++i;
			high = i>0 ? max(intervals[i-1].end, newInterval.end) : newInterval.end;
			Interval temp(low, high);
			result.push_back(temp);
		}
		while (i < size)
		{
			result.push_back(intervals[i]);
			++i;
		}
		return result;
	}
};



另外一种思路是:

先将newInterval插入到intervals,然后再对intervals进行合并。

运用到Merge Intervals的解法。

实现如下:

class Solution {
public:
	static bool comp(const Interval a, const Interval b)
	{
		return a.start < b.start ? 1 : 0;
	}
	vector<Interval> insert(vector<Interval>& intervals,Interval newInterval) {
		intervals.push_back(newInterval);
		vector<Interval> result;
		int size = intervals.size();
		if (size < 2) return intervals;
		sort(intervals.begin(), intervals.end(), comp);
		for (int i = 0; i<size; ++i)
		{
			Interval temp(intervals[i].start, intervals[i].end);
			int high = intervals[i].end;
			while (i < size - 1 && high >= intervals[i + 1].start)
			{
				if (intervals[i + 1].end > high) high = intervals[i + 1].end;
				++i;
			}
			temp.end = high;
			result.push_back(temp);
		}
		return result;
	}
};



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

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
  • 992

【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...

LeetCode 57. Insert Interval/56. Merge Intervals

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

Leetcode 57 Insert Interval

一,问题描述:1,给定一个区间集合,这些区间有些是重合的。另外给一个区间,然后把这些区间,进行合并有重叠的区间2,例如: 给定一个区间集合: [1 3] [6 9] 再给一个区间[2 5...

Leetcode 57. Insert Interval

Leetcode 57. Insert Interval
  • Earl211
  • Earl211
  • 2017年07月19日 22:10
  • 142

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
举报原因:
原因补充:

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