一、题目说明

题目是56. Merge Intervals,给定一列区间的集合,归并重叠区域。

二、我的做法

这个题目不难,先对intervals排序,然后取下一个集合,如果cur[0]>resLast[1]在直接放到集合中,否者合并。代码如下:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution{
public:
vector<vector<int>> merge(vector<vector<int>>& intervals){
int len = intervals.size();
if(len<2) return intervals; sort(intervals.begin(),intervals.end());
vector<vector<int>> res; res.push_back(intervals[0]);
vector<int> cur,resLast; for(int i=1;i<len;i++){
cur = intervals[i];
resLast = res[res.size()-1]; if(cur[0]>resLast[1]){
res.push_back(cur);
}else if(cur[0]<=resLast[1] && cur[1]>resLast[1]){
res.back() = {resLast[0],cur[1]};
}
} return res;
}
};
int main(){
Solution s;
vector<vector<int>> m;
vector<vector<int>> r; m = {{1,3},{2,6},{8,10},{15,18}};
r = s.merge(m);
for(int i=0;i<r.size();i++){
for(int j=0;j<r[i].size();j++){
cout<<r[i][j]<<"->";
}
cout<<"\n";
} cout<<"--------"<<"\n"; m = {{1,4},{4,5}};
r = s.merge(m);
for(int i=0;i<r.size();i++){
for(int j=0;j<r[i].size();j++){
cout<<r[i][j]<<"->";
}
cout<<"\n";
} m = {{1,4},{0,4}};
r = s.merge(m);
for(int i=0;i<r.size();i++){
for(int j=0;j<r[i].size();j++){
cout<<r[i][j]<<"->";
}
cout<<"\n";
} m = {{1,4},{2,3}};
r = s.merge(m);
for(int i=0;i<r.size();i++){
for(int j=0;j<r[i].size();j++){
cout<<r[i][j]<<"->";
}
cout<<"\n";
}
return 0;
}

性能如下:

Runtime: 24 ms, faster than 48.18% of C++ online submissions for Merge Intervals.
Memory Usage: 12.5 MB, less than 82.56% of C++ online submissions for Merge Intervals.

三、优化措施

暂时这样,不优化了。

刷题56. Merge Intervals的更多相关文章

  1. leetcode 56. Merge Intervals 、57. Insert Interval

    56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...

  2. [Leetcode][Python]56: Merge Intervals

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...

  3. 56. Merge Intervals - LeetCode

    Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...

  4. 【LeetCode】56. Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  5. 56. Merge Intervals 57. Insert Interval *HARD*

    1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...

  6. Leetcode#56 Merge Intervals

    原题地址 排序+合并,没啥好说的 第一次尝试C++的lambda表达式,有种写js的感觉,很神奇 c11就支持了lambda表达式,仔细想想,我学C++大概就是在09~10年,c11还没有发布,不得不 ...

  7. 56. Merge Intervals

    题目: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6], ...

  8. LeetCode 56. Merge Intervals (合并区间)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  9. LeetCode OJ 56. Merge Intervals

    题目 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6], ...

随机推荐

  1. IdentityHashCodeTest

    Java学习:identityHashCode和hashCode方法, System类提供了一个identityHashCode(Object x)方法,这个方法返回的是指定对象的精确hashCode ...

  2. Eclipse无法查看第三方jar包文件源代码解决方法

    来源于:https://www.cnblogs.com/1995hxt/p/5252098.html 1.打开第三方依赖包,源文件的快捷键:ctrl + mouseClick 2.由于我们下载的第三方 ...

  3. 基于springboot实现轮询线程自动执行任务

    本文使用: Timer:这是java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行,但不能在指定时 ...

  4. 【PAT甲级】1072 Gas Station (30 分)(Dijkstra)

    题意: 输入四个正整数N,M,K,D(N<=1000,M<=10,K<=10000)分别表示房屋个数,加油站个数,路径条数和加油站最远服务距离,接着输入K行每行包括一条路的两条边和距 ...

  5. SpringMVC Controller 接收页面传递的中文参数出现乱码

    在Controller中接收到的POST参数如果是中文的话,显示为乱码.已知客户端传过来时编码为UTF-8. 问题产生分析: spring MVC中默认的编码格式为“ISO-8859-1”,因此造成乱 ...

  6. 微信公众平台接口获取时间戳为10位,java开发需转为13位

    问题1:为什么会生成13位的时间戳,13位的时间戳和10时间戳分别是怎么来的 ? java的date默认精度是毫秒,也就是说生成的时间戳就是13位的,而像c++或者php生成的时间戳默认就是10位的, ...

  7. DOCK-SWARM

    服务原理: 创建集群:建立ingress网络,网关xxxxx.xxx.xxx.1 管理节点:docker swarm init --advertise-addr 192.168.4.119 工作节点: ...

  8. Redis注意点记录

    场景:1主2从 1.不使用哨兵模式,则当主机宕机后,从机并不会自动切换到Master状态,仍旧是Slave,若主机重新恢复,则从机进行自动连接 2.使用哨兵模式后,主机宕机,从机会根据分配的权值在从机 ...

  9. netty实现websocket客户端(附:测试服务端代码)

    1,客户端启动类 package test3; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.Unpooled; import ...

  10. C++11常用特性介绍——constexpr变量

    一.constexpr变量 1)将变量声明为constexpr类型以便由编译器来验证变量的值是否为一个常量表达式,声明为constexpr的变量一定是一个常量,而且必须用常量表达式来初始化,如: in ...