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. java的一些相关介绍(2013-10-07-163 写的日志迁移

    java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaSE, JavaEE, Jav ...

  2. Form和ModelForm组件

    Form介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否 ...

  3. Lecture 1

    Principles of GIS( UNSW Metternicht ) outline:data input---data management---data manipulation+data ...

  4. numpy 三个点的使用[...]

    numpy [...]语法简单使用 Python numpy中切片功能与列表切片类似,但功能更加强大 本文主讲numpy中[...]的简单使用,后续工作继续补充. import numpy >& ...

  5. 在spring boot中使用webSocket组件(二)

    该篇演示如何使用websocket创建一对一的聊天室,废话不多说,我们马上开始! 一.首先先创建前端页面,代码如下图所示: 1.login.html <!DOCTYPE html> < ...

  6. Spring核心技术(十三)——环境的抽象

    本章将描述一下Spring中针对环境的抽象. Environment是一个集成到容器之中的特殊抽象,它针对应用的环境建立了两个关键的概念:profile和properties. profile是命名好 ...

  7. BZOJ 4896: [Thu Summer Camp2016]补退选

    trie树+vector+二分 别忘了abs(ans) #include<cstdio> #include<algorithm> #include<vector> ...

  8. python - 读取配置文件

    # -*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: read_config.py @ide: PyCharm Commun ...

  9. “万恶”的break

    写这篇随笔主要是希望自己长点记性,break的作用是跳出当次循环,每次用break都有点忘记break后面的条件提前. 正常是这样: exit_flag=Falsefor i in range(10) ...

  10. C++程序在Windows平台上各种定位内存泄漏的方法,并对比了它们的优缺点

    一.前言 在Linux平台上有valgrind可以非常方便的帮助我们定位内存泄漏,因为Linux在开发领域的使用场景大多是跑服务器,再加上它的开源属性,相对而言,处理问题容易形成“统一”的标准.而在W ...