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. 【思维题】TCO14 Round 2C InverseRMQ

    全网好像就只有劼和manchery写了博客的样子……:正解可能是最大流?但是仔细特判也能过 题目描述 RMQ问题即区间最值问题是一个有趣的问题. 在这个问题中,对于一个长度为 n 的排列,query( ...

  2. php 正则表达式中的 .*? 表示什么意思

    我们知道我 .* 是任意字符,有的时候比较困惑在加个?什么意思. ?是非贪婪模式.*会匹配后面的一切字符,就是到结束的意思加?后就是不贪婪模式,这时要看?后边的字符是什么了,如.*?"的意思 ...

  3. MySQL创建数据库,用户,赋予权限

    CREATE DATABASE 'voyager'; CREATE DATABASE `voyager`; CREATE USER 'dog'@'localhost' IDENTIFIED BY '1 ...

  4. 01将图片嵌入到Markdown文档中

    将图片内嵌入Markdown文档中 将图片嵌入Markdown文档中一直是一个比较麻烦的事情.通常的做法是将图片存入本地某个路径或者网络存储空间,使用URL链接的形式插入图片: ![image][ur ...

  5. MySQL 创建函数失败提示1418

    MySQL 创建函数失败提示1418 在创建函数时,往往会遇到创建函数失败的情形,除去书写的创建函数的sql语句本身语法错误之外,还会碰到一个错误就是, 1418:This function has ...

  6. windows下配置Nginx支持php

    编辑配置文件nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; ...

  7. leetcode-16-greedyAlgorithm

    455. Assign Cookies 解题思路: 先将两个数组按升序排序,然后从后往前遍历,当s[j] >= g[i]的时候,就把s[j]分给g[i],i,j都向前移动,count+1;否则向 ...

  8. 网络编程-TCP/IP各层介绍(5层模型讲解)

    1.TCP/IP五层协议讲解 物理层--数据链路层--网络层--传输层--应用层 我们将应用层,表示层,会话层并作应用层,从tcp/ip五层协议的角度来阐述每层的由来与功能,搞清楚了每层的主要协议 就 ...

  9. kafka消息的可靠性

    本文来自网易云社区 作者:田宏增 Kafka的高可靠性的保障来源于其健壮的副本(replication)策略.通过调节其副本相关参数,可以使得Kafka在性能和可靠性之间运转的游刃有余.Kafka从0 ...

  10. 聊聊、Docker 安装