(1)Merge Intervals

https://leetcode.com/problems/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].

思路:贪心思想。首先根据intervals的start进行排序。排完序之后,先将第一个interval加入vector,接着判断之前加入vector的interval和后面一个interval比较。因为是排好序的,所以,只需要判断新的interval的start是否大于前面一个加入vector的interval的end。如果大于,则将后面的interval加入vector,如果不大于,则肯定有交集,两者合并。

struct Interval{
int start;
int end;
Interval():start(),end(){}
Interval(int s,int e):start(s),end(e){}
};
bool cmp(const Interval &left,const Interval & right){
if (left.start == right.start){
return left.end < right.end;
}else{
return left.start < right.start;
}
}
class Solution {
public:
/*struct cmp{
bool operator()(const Interval &left,const Interval & right){
if (left.start == right.start){
return left.end < right.end;
}else{
return left.start < right.start;
}
}
};*/ vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> result;
if (intervals.size() == || intervals.empty()){
return result;
}
sort(intervals.begin(),intervals.end(),cmp);
result.push_back(intervals[]);
int index = ;
for (int i = ; i < intervals.size(); i++){
if (intervals[i].start <= result[index].end){
int end = max(intervals[i].end,result[index].end);
result[index].end = end;
//index++;
//result.push_back(intervals[i]);
}else{
result.push_back(intervals[i]);
index++;
}
}
return result;
}
};

(2)Insert Interval

https://leetcode.com/problems/insert-interval/

Title:

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

思路:我最直接的想法就是先找到newInterval的start在哪两个intervals之间,然后再找到end。虽然也能做,但是代码很复杂,不精练

class Solution {
public:
vector<Interval> insert(vector<Interval> & intervals,
Interval newInterval) {
vector<Interval> result;
if (intervals.size() == || intervals.empty()){
result.push_back(newInterval);
return result;
}
int i;
bool flag = false;
for (i = ; i < intervals.size();) {
if (intervals[i].start >= newInterval.start) {
int start, end;
if (i == ) {
start = newInterval.start;
} else {
if (intervals[i-].end >= newInterval.start){
start = intervals[i - ].start;
result.pop_back();
}else
start = newInterval.start; }
while (i < intervals.size() && intervals[i].start <= newInterval.end) {
i++;
}
if(i == intervals.size()){
end = max(intervals[i-].end,newInterval.end);
Interval interval(start,end);
result.push_back(interval);
}else{
end = max(intervals[i - ].end, newInterval.end);
Interval interval(start, end);
result.push_back(interval);
for (int j = i; j < intervals.size(); j++)
result.push_back(intervals[j]);
}
flag = true;
break;
} else {
result.push_back(intervals[i]);
i++;
}
}
if (!flag){
if (intervals[i-].end < newInterval.start){
//result.push_back(intervals[i-1
result.push_back(newInterval);
}else{
result.pop_back();
int start = intervals[i-].start;
int end = max(intervals[i-].end,newInterval.end);
Interval interval(start,end);
result.push_back(interval);
}
} return result;
} };

然后看了网上其他人的想法

Quickly summarize 3 cases. Whenever there is intersection, created a new interval.

遍历一遍vector,根据上述三种情况考虑添加。

vector<Interval> insert(vector<Interval> &intervals, Interval newInterval){
vector<Interval> result;
for (int i = ; i < intervals.size(); i++){
if (intervals[i].end < newInterval.start){
result.push_back(intervals[i]);
}else if (intervals[i].start > newInterval.end){
result.push_back(newInterval);
newInterval = intervals[i];
}else{
newInterval = Interval(min(intervals[i].start,newInterval.start),max(intervals[i].end,newInterval.end));
}
}
result.push_back(newInterval);
return result;
}

LeetCode: Interval的更多相关文章

  1. [LeetCode] Find Right Interval 找右区间

    Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...

  2. [LeetCode] Insert Interval 插入区间

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

  3. Leetcode: Find Right Interval

    Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...

  4. Java for LeetCode 057 Insert Interval

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

  5. [LeetCode]题解(python):057-Insert Interval

    题目来源 https://leetcode.com/problems/insert-interval/ Given a set of non-overlapping intervals, insert ...

  6. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

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

  7. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  8. [Leetcode] Binary search--436. Find Right Interval

      Given a set of intervals, for each of the interval i, check if there exists an interval j whose st ...

  9. 【一天一道LeetCode】#57. Insert Interval

    一天一道LeetCode系列 (一)题目 Given a set of non-overlapping intervals, insert a new interval into the interv ...

随机推荐

  1. ajax与jsonp的区别

    ajax的核心是通过XmlHttpRequest获取非本页内容,而jsonp的核心则是动态添加<script>标签来调用服务器提供的js脚本.

  2. linux服务器重启服务命令说明文档

    (前提是电脑上面已经安装好了ssh软件~!)输入ip,用户名,端口(默认22) 输入密码,登陆成功之后, 转入到/usr/local/tomcat/bin 目录,输入命令行: [root@yangch ...

  3. hadoop浅尝 第一个hadoop程序

    hadoop编程程序员需要完成三个类. map类,reduce类和主类. map和reduce类自然是分别完成map和reduce.而主类则负责对这两个类设置job.完成这三个类之后,我们生成一个ja ...

  4. POJ 2299 Ultra-QuickSort (排序+数据离散化+求顺序数)

    题意:给出一个序列,求至少需要用到多少次操作,才能将序列从小到大排序. 思路: 数据量很大,n<500000,所以用冒泡模拟是超时的. 接下来就想到了求顺序数,总共需要交换的次数为每个数后面有多 ...

  5. POJ2632Crashing Robots

    做模拟题做的我直接睡着了,题并不难,就是一个细心的问题,有一些细节问题注意了就差不多了,代码写的精美的一般找错误也好找一些,应该学着些好看的代码 #include<cstdio> #inc ...

  6. hdu 4335 What is N?

    此题用到的公式:a^b%c=a^(b%phi(c)+phi(c))%c (b>=phi(c)). 1.当n!<phi(p)时,直接暴力掉: 2.当n!>=phi(p) &&a ...

  7. http://www.oschina.net/translate/elasticsearch-getting-started?cmp

    http://www.oschina.net/translate/elasticsearch-getting-started?cmp

  8. springmvc环境的搭建

    最近应公司要求,用了2天时间学了springmvc的搭建,就简单总结一下: springmvc和struts2的比较,因为我是学过struts的,它们都是基于mvc模式而设计的web层框架 它们最大的 ...

  9. SqlDataAdapter用法

    SqlDataAdapter和SqlCommand区别: SqlCommand就是是命令了,可以用它来执行SQL命令: SqlDataAdapter就是数据适配器了,它是用于在数据源和数据集之间通讯的 ...

  10. C#实现字符串按多个字符采用Split方法分割

    原文:C#实现字符串按多个字符采用Split方法分割 String字符串如何按多个字符采用Split方法进行分割呢?本文提供VS2005和VS2003的实现方法,VS2005可以用下面的方法: str ...