LeetCode OJ--Insert Interval **
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 **的更多相关文章
- [OJ] Insert Interval
LintCode #30. Insert Interval (Easy) LeetCode #57. Insert Interval (Hard) class Solution { public: v ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- 【leetcode】Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- Leetcode: Merge/Insert Interval
题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- 第一周 Leetcode 57. Insert Interval (HARD)
Insert interval 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
- 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 ...
- Java for LeetCode 057 Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- LeetCode: 57. Insert Interval(Hard)
1. 原题链接 https://leetcode.com/problems/insert-interval/description/ 2. 题目要求 该题与上一题的区别在于,插入一个新的interva ...
- [LeetCode] 57. Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
随机推荐
- 【最大流】bzoj1711: [Usaco2007 Open]Dining吃饭
正在网络流入门(原来这种题用网络流做) Description 农夫JOHN为牛们做了很好的食品,但是牛吃饭很挑食. 每一头牛只喜欢吃一些食品和饮料而别的一概不吃.虽然他不一定能把所有牛喂饱,他还是想 ...
- python爬虫: 豆瓣电影top250数据分析
转载博客 https://segmentfault.com/a/1190000005920679 根据自己的环境修改并配置mysql数据库 系统:Mac OS X 10.11 python 2.7 m ...
- Vue木桶布局插件
公司最近在重构,使用的是Vue框架.涉及到一个品牌的布局,因为品牌的字符长度不一致,所以导致每一个的品牌标签长短不一.多行布局下就会导致每行的品牌布局参差不齐,严重影响美观.于是就有了本篇的木 ...
- Linux远程传输命令scp
指令:scp在不同的linux主机间复制文件带有Security的文件copy,基于ssh登录. 有些linux发行版没有自带scp,因此需要安装scp# yum -y install openssh ...
- zend studio10 破解方法
zend studio 是一款强大的PHP开发工具,该软件的具体信息用户可以百度一下,下面是zend studio10的破解方法. 1.下载zend studio 10,下载地址:链接:http:// ...
- 树莓派开发板入门学习笔记1:[转]资料收集及树莓派系统在Ubuntu安装
参考教程(微雪课堂):http://www.waveshare.net/study/portal.php 树莓派实验室: http://shumeipai.nxez.com/2014/12/21/us ...
- 欧拉函数:HDU3501-Calculation 2
Calculation 2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- python基础学习笔记——正则表达式
1.什么是正则? 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.(在Python中)它内嵌在Python中,并通过 r ...
- python基础补漏-09-反射
isinstance class A: passclass B(A): pass b = B()print isinatance(b,A)issubclass 判断某一个类是不是另外一个类的派生类 # ...
- SVM用于线性回归
SVM用于线性回归 方法分析 在样本数据集()中,不是简单的离散值,而是连续值.如在线性回归中,预测房价.与线性回归类型,目标函数是正则平方误差函数: 在SVM回归算法中,目的是训练出超平面,采用作为 ...