[LeetCode] Merge Intervals 排序sort
这题其实想好思路很好解决,对于框,如果下个框开始在 其中间,则连在一起,否则单独为一个,这需要按start 排序便可以了,因为类中写自定义比较函数比较麻烦,所以一次写了好几个。
- 按start 排序
- 初始化变量curstart,curend,记录当前窗的位置。
- 与下个窗比较,如果其start < curend,更新 curend。
- 否则加入ret,并跟新curstart,curend
- 遍历结束,加入最后的窗。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; /**
* Definition for an interval.
*/
struct Interval {
int start;
int end;
Interval() : start(), end() {}
Interval(int s, int e) : start(s), end(e) {}
}; class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
sort(intervals.begin(),intervals.end(),\
[] (Interval i1,Interval i2)\
{return i1.start<i2.start; }); /// sort(intervals.begin(),intervals.end(),help_fun); /** struct my_cmp{
bool operator ()(Interval i1,Interval i2){
return i1.start<i2.start;
}
}my_cmp1;
sort(intervals.begin(),intervals.end(),my_cmp());
sort(intervals.begin(),intervals.end(),my_cmp1);
*/ // for(int i=0;i<intervals.size();i++){
// cout<<intervals[i].start<<" "<<intervals[i].end<<endl;
// }
vector<Interval> ret;
if(intervals.size()<) return ret;
int curStart=intervals[].start,curEnd=intervals[].end;
for(int i=;i<intervals.size();i++){
if(curEnd>=intervals[i].start){
if(intervals[i].end>curEnd) curEnd=intervals[i].end;
continue;
}
ret.push_back(Interval(curStart,curEnd));
curStart = intervals[i].start;
curEnd = intervals[i].end;
}
ret.push_back(Interval(curStart,curEnd));
return ret;
} static bool help_fun(Interval i1,Interval i2)
{
return i1.start<i2.start;
}
}; int main()
{
vector<Interval> intervals={Interval(,),\
Interval(,),\
Interval(,),\
Interval(,)};
Solution sol;
vector<Interval> ret = sol.merge(intervals); for(int i=;i<ret.size();i++){
cout<<ret[i].start<<" "<<ret[i].end<<endl;
}
return ;
}
[LeetCode] Merge Intervals 排序sort的更多相关文章
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- LeetCode: Merge Intervals 解题报告
Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [ ...
- [leetcode]Merge Intervals @ Python
原题地址:https://oj.leetcode.com/problems/merge-intervals/ 题意: Given a collection of intervals, merge al ...
- Leetcode Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 56. Merge Intervals (Array; Sort)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。
感觉有一点进步了,但是思路还是不够犀利. /** * Definition for an interval. * struct Interval { * int start; * int end; * ...
- 56[LeetCode] .Merge Intervals
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- 【LeetCode】Merge Intervals 题解 利用Comparator进行排序
题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
随机推荐
- linux常用指令学习记录
前言 本文主要为学习贴,用来记录一些 linux上的常用指令 以供参考. 文件内容查看 cat 从上往下阅读文件内容 cat [-AbEnTv] ${FILE_NAME) cat -n /etc/is ...
- thinkcmf5 iis+php重写配置
TP在本机运行非常好,谁想到服务器上后,连http://www.***.com/wap/login/index都404错误了, 中间的郁闷过程不表. 解决方案分两步: 第一步: 下载rewrite_2 ...
- vue.js 图片预览
Vue.js的图片预览的插件还是不少,但是找了半天还是没找到跟现在项目里能用得很顺手的,其实项目里图片预览功能很简单,点击放大,能双指缩放就可以了.部分vue.js的图片预览库都需要把图片资源单独拿出 ...
- JZOJ 5775. 【NOIP2008模拟】农夫约的假期
5775. [NOIP2008模拟]农夫约的假期 (File IO): input:shuru.in output:shuru.out Time Limits: 1000 ms Memory Lim ...
- JZOJ 1266. 玉米田
1266. 玉米田(cowfood.pas/c/cpp) (File IO): input:cowfood.in output:cowfood.out Time Limits: 1000 ms Me ...
- Ueditor百度编辑器插件的安装
目录 插件下载地址: 1. 引入 2. 编辑器显示处 id="content" 3.底部 4.ueditor编辑器按钮配置方法 定制工具栏图标 修改配置项的方法: 插件下载地址: ...
- hive数据的导入导出方式
导入方式 1.load方式 load data local inpath 'local_path' into table tb_name; 从本地复制了文件到表的路径下 应用场景:大部分的使用,文件几 ...
- Cube HDU - 1220(思维)
Cowl is good at solving math problems. One day a friend asked him such a question: You are given a c ...
- python深浅拷贝以及数据在内存中储存方法
要搞懂深浅拷贝,首先要明白数据在内存里的储存方法. 一个变量的储存,首先是变量名加上储存内容的ID,通过ID去找到变量名所对应的内容, 当我们对数据进行赋值时,其实是把内容的整体地址赋给别的变量名(相 ...
- 文本处理grep命令
this is a words file. words words to be , , , , , , , , , , beginning linux programming 4th edition ...