Leetcode#57 Insert Interval
遍历每个区间intervals[i]:
如果intervals[i]在newInterval的左边,且没有交集,把intervals[i]插入result
如果intervals[i]在newInterval的右边,且没有交集,如果newInterval还没插入,则将newInterval插入result,之后再插入intervals[i]
如果intervals[i]和newInterval有交集,则更新newInterval的start、end
代码:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
vector<Interval> result;
bool inserted = false; for (auto itv : intervals) {
if (itv.end < newInterval.start)
result.push_back(itv);
else if (itv.start > newInterval.end) {
if (!inserted) {
result.push_back(newInterval);
inserted = true;
}
result.push_back(itv);
}
else {
newInterval.start = min(itv.start, newInterval.start);
newInterval.end = max(itv.end, newInterval.end);
}
}
if (!inserted)
result.push_back(newInterval); return result;
}
Leetcode#57 Insert Interval的更多相关文章
- 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 ...
- [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 解决思路
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 ...
随机推荐
- spark-submit工具参数说明
执行时需要传入的参数说明 Usage: spark-submit [options] <app jar | python file> [app options] 参数名称 含义 --mas ...
- php codebase生成随机数
php使用codebase生成随机数. 有25幅作品拿去投票,一次投票需要选16幅,单个作品一次投票只能选择一次.前面有个程序员捅了漏子,忘了把投票入库,有200个用户产生的投票序列为空.那么你会如 ...
- PHP 只有登陆后才能浏览的简单实现
1.============================================================= 在你不想让别人直接进入的网页开头加一段代码: session_start ...
- Python多版本安装 Python2.7和Python3.5
声明:本文仅仅在win8.1测试通过! 1.下载 Python2.7,3.5 2.依次安装Python27(c:\Python27) Python35(c:\Python35) 3.c:\Pytho ...
- JavaWeb之Servlet:Cookie 和 Session
会话 现实生活中我们会用手机跟对方对话,拿起手机,拨号,然后对面接听,跟着互相通话,最后会话结束. 这个过程也可以用我们的B/S模式来描述: 打开浏览器—>输入地址->发出请求->服 ...
- NFC Forum : Frequently Asked Questions (NFC 论坛:FAQ)
NFC for Business What is the NFC Forum? The NFC Forum is a not-for-profit industry organization whos ...
- hdu 2094 产生冠军
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2094 产生冠军 Description 有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比 ...
- Bing Speech Recognition 标记
Bing Speech Services Bing Bing Speech Services provide speech capabilities for Windows and Windows ...
- 使用golang+java实现基于ecb的3eds加解密
http://www.100hack.com/2014/04/14/golang%E4%B8%AD%E7%9A%84des%E5%8A%A0%E5%AF%86ecb%E6%A8%A1%E5%BC%8F ...
- hive hwi使用
hwi(hive web interface)是hive命令行接口的补充. 使用方法: 1.配置: 在配置文件hive-site.xml 中,默认有hwi的配置 <property> &l ...