(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. .net发送邮件代码示例

    下面的代理已经调试过,用的是163的SMTP using System;using System.Collections.Generic;using System.Linq;using System. ...

  2. gdb基本使用方法

    gdb时linux下的一个非常好用的调试工具.下面给出它几个常用的方法 b 设置断点.c 继续执行. i 查看一些信息,比如断点,i b. bt 查看函数调用栈. n 执行下一条指令,但不会进入到调用 ...

  3. UVA 10523 Very Easy!!!(大数据加法、乘法)

    题意:给出N,A,计算i*A^i(i=1~N)的和.1<=N<=30,0<=A<=15. 就是大数据运算,别的没什么,注意细节之处即可. 这题还要注意两个地方: 1.考虑A=0 ...

  4. 【互联网那些事儿】小度 i 耳目

    关于这个产品是什么,大家自行度. 这里我主要想说的,是百度关于这个产品的一点……呃,“卖萌”的介绍语言. 小度i耳目常见问题 问:为什么叫小度i耳目呢,貌似不太好记忆. 答:名字嘛都是父母起的,不过时 ...

  5. DP方程及意义

    01背包 有N件物品和一个容量为V的背包.第i件物品的费用(即体积,下同)是w[i],价值是c[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 基本思路: 这是最基 ...

  6. javaWEB邮件测试

    新建一个工具类: Mail.java 该类的主要关键点是:1.设置系统属性.也就是你是用什么协议来进行邮件发送的,邮件协议有很多在种,比如impt,smpt,prop等协议, 我现在测试用的是smpt ...

  7. 数据挖掘10大算法(1)——PageRank

    1. 前言 这系列的文章主要讲述2006年评出的数据挖掘10大算法(见图1).文章的重点将偏向于算法的来源以及算法的主要思想,不涉及具体的实现.如果发现文中有错,希望各位指出来,一起讨论. 图1 来自 ...

  8. Socket称"套接字"

    Socket又称"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求. 二.利用Socket建立网络连接的步骤 建立Socket连接至少需要一对 ...

  9. Java Project和Web Project 区别

    java project是java工程,不包括JSP等前台页面的代码 大部分是CS结构的工程和一些jar包 web project是web工程,是BS结构的系统 web project部署到服务器上 ...

  10. Android:单元测试

    通过单元测试的方法可以轻松判断BUG 第一步:首先在AndroidManifest.xml中加入下面红色代码: 打开AndroidManifest.xml,选择instrumentation ,选择N ...