第一周 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 ...
随机推荐
- 并发和多线程(二)--线程安全、synchronized、CAS简介
线程安全性: 当多个线程访问一个类的时候,这个类始终表示出正确的行为,那么这个类是线程安全的. 无状态的对象一定是线程安全的,例如大部分service.dao.Servlet都是无状态的. 线程安全体 ...
- 迷宫自动生成以及基于DFS的自动寻路算法
直接贴代码 #include<ctime> #include<conio.h> #include<iostream> #include<windows.h&g ...
- vue-cli 3.x 配置多环境
思路:新建一个 process.env 变量. 把 webpack 配置放到 vue.config.js 里面. 如果根目录下没有该文件,新建.配置参考:https://cli.vuejs.org/z ...
- vue-cli中src/main.js 的作用
// The Vue build version to load with the `import` command // (runtime-only or standalone) has been ...
- Codeforces 989C - A Mist of Florescence
传送门:http://codeforces.com/contest/989/problem/C 这是一个构造问题. 构造一张网格,网格中的字符为’A’.’B’.’C’.’D’,并且其连通块的个数分别为 ...
- hdu 4670 树的分治-求点对的个数
/* 树的分治 因为树的点权值可达到10^15,注意手动扩栈,还有int64 题意:给你一棵树,给你一些素数,给你每个点一个权值且每个权值均可由这些素数组成.现在定义任意任意两点的价值为他们路径上的权 ...
- 个人常用git命令
最近开始使用git,将自己常用git命令做一个简单归纳,便于记忆. 初始化及配置 git init:初始化资料库 git config --global user.name 'xxx':配置用户名 g ...
- vim高亮显示当前行列
vim高亮显示当前行: set cursorline vim高亮显示当前列: set cursorcolumn
- 特种部队(codevs 1427)
题目描述 Description 某特种部队接到一个任务,需要潜入一个仓库.该部队士兵分为两路,第一路士兵已经在正面牵制住了敌人,第二路士兵正在悄悄地从后方秘密潜入敌人的仓库.当他们到达仓库时候,发现 ...
- IT领域的罗马帝国——微软公司
微软公司从做软件开始,起步很小.但是盖茨确是一直深耕于战略布局,像一个棋局高手,每一步棋都是看了后面几步. 盖茨居然用9年的时间憋出一个win3.0,成功击败了apple. 而这9年拖住apple的居 ...