第一周 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 ...
 
随机推荐
- Spring框架系列(七)--Spring常用注解
			
Spring部分: 1.声明bean的注解: @Component:组件,没有明确的角色 @Service:在业务逻辑层使用(service层) @Repository:在数据访问层使用(dao层) ...
 - P1077摆花
			
传送 总共要摆m盆花,而每种花最多有a[i]盆.仔细思索,发现它是一个多重背包求方案数问题.但是我蒟蒻的不会,于是跑去问大佬. 以下状态转移方程及化简from rqy 如果第i个物品有a[i],每个的 ...
 - 用Java写一个生产者-消费者队列
			
生产者消费者的模型作用 通过平衡生产者的生产能力和消费者的消费能力来提升整个系统的运行效率,这是生产者消费者模型最重要的作用. 解耦,这是生产者消费者模型附带的作用,解耦意味着生产者和消费者之间的联系 ...
 - 打造完美的ImageLoader——LruCache+DiskLruCache
			
做android应用少不了要和网络打交道,在我刚开始学android的时候总是处理不好网络图片的加载,尤其是图片乱跳的问题,后来发现了各种缓存图片的方法:本地缓存.软引用.LruCache.... 我 ...
 - 在Java中调用带参数的存储过程
			
JDBC调用存储过程: CallableStatement 在Java里面调用存储过程,写法那是相当的固定: Class.forName(.... Connection conn = DriverMa ...
 - with一个对象,自动触发__enter__方法
			
class Foo(object): def __init__(self): pass def __enter__(self): print("__enter__") def __ ...
 - 完美解决在Servlet中出现一个输出中文乱码的问题
			
@Override public void doPost(HttpServletRequest reqeust, HttpServletResponse response) throws Servle ...
 - https://segmentfault.com/a/1190000012844836---------关于SpringBoot上传图片的几种方式
			
关于SpringBoot上传图片的几种方式 https://segmentfault.com/a/1190000012844836
 - noip模拟赛 蒜头君的坐骑
			
分析:标准的棋盘dp问题. 如果没有技能,那么就很好做了,相当于传纸条的做法.有了技能的限制,我们就要加上一维表示用了多少次技能,这个时候转移就要用到dfs了,而且不能用填表法,要用刷表法,从当前位置 ...
 - So easy
			
Problem Description Small W gets two files. There are n integers in each file. Small W wants to know ...