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

给出有序的区间来,再插入进去一个,也就是区间合并。

刚开始确立了几个思路,看要插入的区间的start和end分别位于什么位置。区分是在空闲处还是在一个区间里面,并且还要判断跨越了几个区间,并且要进行数组元素的后移之类的。写了代码比较麻烦,于是想新的思路。如下:新建立一个数组,这样从前往后扫描老数组和要插入元素的关系,如果还没到插入元素,则正常将老数组元素复制到新数组里面,如果到了要插入的元素,则开始看,哪个begin更小,则用哪个,之后再while循环,一直到找到end的关系。当处理完要插入的元素后,剩下的全部拷贝到新数组中。在思考的过程中,在纸上画出所有位置相关可能的情况。

1.要插入的元素完整在本次处理元素的左边,则插入要插入的元素,并且相当于处理完了。

2.要插入的元素完整在本次处理元素的右边,则继续往后扫描。

3.要插入元素在一个区间的里面,相当于处理完了,不用插入。

4.其他位置关系,则要找到开始位置,然后再处理结束位置。

#include <iostream>
#include <vector>
using namespace std; struct Interval {
int start;
int end;
Interval() : start(), end() {}
Interval(int s, int e) : start(s), end(e) {}
}; class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) { if(intervals.size()== ||newInterval.start>intervals[intervals.size()-].end)
{
intervals.push_back(newInterval);
return intervals;
}
vector<Interval> intervals_ans;
bool flag_finish = false;
for(unsigned int i = ;i < intervals.size();i++)
{
if(flag_finish == false)
{
if(intervals[i].start > newInterval.end)
{
intervals_ans.push_back(newInterval);
flag_finish = true;
}
else if(intervals[i].end < newInterval.start)
intervals_ans.push_back(intervals[i]);
else if(intervals[i].start<=newInterval.start && intervals[i].end >= newInterval.end)
flag_finish = true;
else
{
int newbegin;
int newend;
if(intervals[i].start>=newInterval.start)
newbegin = newInterval.start;
else if(intervals[i].start<newInterval.start)
newbegin = intervals[i].start; unsigned int j = i + ;
while(j< intervals.size() && intervals[j].start<=newInterval.end)
j++;
if(intervals[j-].end <= newInterval.end)
newend = newInterval.end;
else if(intervals[j-].end > newInterval.end)
newend = intervals[j-].end;
Interval in;
in.start = newbegin;
in.end = newend;
intervals_ans.push_back(in);
i = j;
flag_finish = true;
}
}
if(flag_finish == true && i<intervals.size())
intervals_ans.push_back(intervals[i]);
}
return intervals_ans;
}
}; int main()
{
Interval i1(,);
Interval i2(,); vector<Interval> intervals;
intervals.push_back(i1);
intervals.push_back(i2); Interval i(,);
Solution myS;
myS.insert(intervals,i);
return ;
}

另外get了一点,要处理warning:

程序执行的时候,在insert函数调用后就崩了,在vector的析构函数中“

> ConsoleApplication3.exe!std::vector<Interval,std::allocator<Interval> >::~vector<Interval,std::allocator<Interval> >() 行 901 C++

”即发生了内存泄露,在最后清理的时候挂了。

函数执行前有如下warning信息:

>d:\documents\visual studio \projects\consoleapplication3\consoleapplication3\源.cpp(): warning C4018: “<”: 有符号/无符号不匹配
>d:\documents\visual studio \projects\consoleapplication3\consoleapplication3\源.cpp(): warning C4018: “<”: 有符号/无符号不匹配
>d:\documents\visual studio \projects\consoleapplication3\consoleapplication3\源.cpp(): warning C4715: “Solution::insert”: 不是所有的控件路径都返回值

于是,把warning都解决了,发现原来没有写最后的 “return intervals_ans;” ,加上就好了。

以后不可以无视 warning!

LeetCode OJ--Insert Interval **的更多相关文章

  1. [OJ] Insert Interval

    LintCode #30. Insert Interval (Easy) LeetCode #57. Insert Interval (Hard) class Solution { public: v ...

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

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

  3. 【leetcode】Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  4. Leetcode: Merge/Insert Interval

    题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...

  5. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

  6. 第一周 Leetcode 57. Insert Interval (HARD)

    Insert interval  题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...

  7. leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval

    lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...

  8. Java for LeetCode 057 Insert Interval

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

  9. LeetCode: 57. Insert Interval(Hard)

    1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...

  10. [LeetCode] 57. Insert Interval 插入区间

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

随机推荐

  1. 【思维题 欧拉图】loj#10106. 单词游戏

    巧妙的模型转化 题目描述 来自 ICPC CERC 1999/2000,有改动. 有 NNN 个盘子,每个盘子上写着一个仅由小写字母组成的英文单词.你需要给这些盘子安排一个合适的顺序,使得相邻两个盘子 ...

  2. 深入理解 hashcode 和 hash 算法

    深入理解 hashcode 和 hash 算法 2017年12月30日 23:06:07 阅读数:5197 标签: hashhashmaphashcode二进制 更多 个人分类: jdk-源码  ht ...

  3. centos7.4进入单用户模式

    centos7.4进入单用户模式 1 - 在启动grub菜单,选择编辑选项启动 2 - 按键盘e键,来进入编辑界面 3 - 找到Linux 16的那一行,将ro改为rw init=/sysroot/b ...

  4. http客户端与浏览器的区别

    两者区别:浏览器对http响应头会进行特定处理(如自动读取本地缓存.设置cookie等),而http客户端(如crul)可能没有像浏览器那样的处理,某些封装程度高的http客户端,可能会有. 同一个文 ...

  5. GoF23种设计模式之行为型模式之模板方法

    概述 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中. TemplateMethod使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 适用性 1.一次性实现一个算法的不变的部分, ...

  6. java web项目(spring项目)中集成webservice ,实现对外开放接口

    什么是WebService?webService小示例 点此了解 下面进入正题: Javaweb项目(spring项目)中集成webservice ,实现对外开放接口步骤: 准备: 采用与spring ...

  7. socketserver的使用

    socketserver底层也是使用线程实现的并发,直接上代码 # server import socketserver ''' socketserver使用模式: 1 功能类 class Myser ...

  8. aoj-0118 property distribution(搜索)

    Time limit1000 ms Memory limit131072 kB タナカ氏が HW アールの果樹園を残して亡くなりました.果樹園は東西南北方向に H × Wの区画に分けられ.区画ごとにリ ...

  9. Monkeyrunner脚本的录制与回放

    继上一篇monkeyrunner环境搭建:http://www.cnblogs.com/zh-ya-jing/p/4351245.html 之后,我们可以进一步学习monkeyrunner了. 我也是 ...

  10. alex 推荐的书

     这两本书不错, 追风筝的人<白鹿原>~~~反天不错~~~可以看下.14:27:22AndyZhang 2018-1-29 14:27:22 改变人的东西  读书.看电影.旅行.经历各种事 ...