第一周 Leetcode 57. Insert Interval (HARD)
题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合。
只想到了一个线性的解法,所有区间端点,只要被其他区间覆盖,就是不合法的,把他们去掉后,就可以直接得到答案。设新区间为【left,right】,那么,比left小的端点显然合法,比right大的端点显然也合法,left和right之间的端点显然不合法,然后判断Left和right是否合法即可。难度不大,代码有些细节需要注意。
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
class Solution {
public:
vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) {
vector<int> vec;
for(int i=0;i<intervals.size();i++)
{
vec.push_back(intervals[i].start);
vec.push_back(intervals[i].end);
}
int left=newInterval.start,right=newInterval.end;
bool el=true,er=true;
int i=0;
vector<int>ans;
while(i<vec.size()&&vec[i]<left)
{
ans.push_back(vec[i]);
i++;
}
i--;
while(i<0)i+=2;
if((i%2)==1) ans.push_back(left);
i=vec.size()-1;
while(i>=0&&vec[i]>right)
i--;
i++;
if((i%2)==0) ans.push_back(right);
while(i<vec.size())
{
ans.push_back(vec[i]);
i++;
}
vector<Interval> anss;
i=0;
while(i<ans.size())
{
Interval nn(ans[i],ans[i+1]);
anss.push_back(nn);
i+=2;
}
return anss; }
};
第一周 Leetcode 57. Insert Interval (HARD)的更多相关文章
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- 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 ...
- [LeetCode] 57. 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 插入区间 (C++/Java)
题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nec ...
- Leetcode#57 Insert Interval
原题地址 遍历每个区间intervals[i]: 如果intervals[i]在newInterval的左边,且没有交集,把intervals[i]插入result 如果intervals[i]在ne ...
- [LeetCode] 57. Insert Interval 解决思路
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [leetcode]57. Insert Interval插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- Leetcode 57: Insert Interval 让代码更好读, 更容易测试.
阅读了几个博客, 决定写一个简易版本; 忙着做更多题, 没有时间多考虑优化代码, 所以, 就写一个试试运气. http://blog.csdn.net/kenden23/article/details ...
随机推荐
- 解决vue项目运行过程中,npm run dev 报错问题
[方案1] 错误如下: npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! travel@1.0.0 dev: `webpack-dev-server ...
- 用 Systemtap 统计 TCP 连接
转自: https://mp.weixin.qq.com/s?__biz=MzIxMjAzMDA1MQ==&mid=2648946009&idx=1&sn=3a0be2fe4f ...
- 小程序button默认样式透彻理解
微信小程序有一个默认样式,特别是有一个外边框,虽然看起来不别扭,但是自己每次设置border:0:都不生效,写成内联的样式也不生效,后来才知道里面的边框是伪元素的边框,这里的伪元素可以理解为用css动 ...
- Game Rank(NCPC 2016 大模拟)
题目: The gaming company Sandstorm is developing an online two player game. You have been asked to imp ...
- Python面向对象,析构继承多态
析构: def __del__(self): print("del..run...") r1 = Role("xx") del r1 结果打印del..run. ...
- Scoop - 在Windows命令行上进行程序安装
2019-01-28 22:49:21 资料来源自Scoop官方网站以及github上的帮助文档 如果有疑惑或者觉得文章有错误请留言以帮助改正 补充内容(2019-04-09 21:11:36):不 ...
- 第一章 Linux命令行简介
1 Linux系统命令操作语法的格式 命令_[参数选项]_[文件或路径] 其中 _ 至少一个空格 如:rm -f /etc/hosts 其中/etc/hosts完整路径不带空格 ...
- C++标准库:bitset 用法整理&&zoj 3812
转载: http://happyboy200032.blog.163.com/blog/static/46903113201291252033712/ 头文件:#include <bits/st ...
- [bzoj2038][2009国家集训队]小Z的袜子(hose)_莫队
小Z的袜子 hose 2009-国家集训队 bzoj-2038 题目大意:给定一个n个袜子的序列,每个袜子有一个颜色.m次询问:每次询问一段区间中每种颜色袜子个数的平方和. 注释:$1\le n,m\ ...
- MySQL主从复制邮件报警脚本
#!/bin/shexport PATH=$PATH:/application/mysql/3306/binlogFile=`date +"%Y-%m-%d %H:%M:%S"`_ ...