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. Linux 常用命令(三)

    一.less --分页查看文件:方面查阅(编辑)大文件 说明:支持方向键盘和鼠标向上向下浏览 -N 显示行号 二.head --output the first  part of files 默认显示 ...

  2. Python基础学习总结__Day2

    一.模块初始 1.标准库模块: (1) Os模块 ① 和操作系统交互:例:执行命令代码 (2) Sys模块 ① 脚本+参数——>结果 2.第三方库模块:Django,Mysql... 存在E:\ ...

  3. LeetCode(242)Valid Anagram

    题目 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...

  4. day08 多线程socket 编程,tcp粘包处理

    复习下socket 编程的步骤: 服务端:   1 声明socket 实例 server = socket.socket()  #括号里不写  默认地址簇使用AF_INET  即 IPv4       ...

  5. CI 分页类的使用

    分页本身很简单,无非就是一个 [limit $offset, $length] 的过程. $length 是每页显示的数据量,这个是固定的.要确定的就只有 $offset了. 在CI中的分页类同样要依 ...

  6. Xpath - Xpath定位

     selenium 提供的xpath定位方法名为:find_element_by_xpath(xpath表达式) Xpath基本定位语法: / 绝对定位,从根节点选取 // 相对定位,从匹配选择的当前 ...

  7. python week08 并发编程之多线程--实践部分

    一. threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性,因而不再详细介绍 官网链接:https://docs.pytho ...

  8. mongo以及mysql常用语句

    db.user.find({']}}).pretty(); db.user.find({primaryPhone:).forEach(function(user){print('my phonenum ...

  9. [python学习篇][廖雪峰][1]高级特性--创建生成器 方法2 yield

    def fib(max): n, a, b = 0, 0, 1 while n < max: print b a, b = b, a + b n = n + 1 将print b 改成yield ...

  10. 【bzoj2561】最小生成树 网络流最小割

    题目描述 给定一个边带正权的连通无向图G=(V,E),其中N=|V|,M=|E|,N个点从1到N依次编号,给定三个正整数u,v,和L (u≠v),假设现在加入一条边权为L的边(u,v),那么需要删掉最 ...