leetcode || 56、 Merge Intervals
problem:
thinking:
(1)一開始我想到用hash table的方法,开一个总区间跨度的数组。对于有区间覆盖的数组区间置为true。没被覆盖的数组区间置为false,最后将true区间的起点和终点作为区间输出就可以。
思路简单。可是,我忽略一个问题:区间跨度是不定的,所以要开的数组大小有可能非常大。提交也显示:Memory
Limit Exceeded
(2)换一种方法。排序法。
能够直接对vector<Interval> 数组排序,要重载compare函数。也能够使用multimap<int, int>,注意不是map
code:
排序法: Accepted
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> ret;
multimap<int,int> map_intervals;
if(intervals.size()==0)
return ret;
if(intervals.size()==1)
return intervals;
for(vector<Interval>::iterator it=intervals.begin();it!=intervals.end();it++)
map_intervals.insert(make_pair((*it).start,(*it).end));
multimap<int, int>::iterator p=map_intervals.begin();
Interval tmp(p->first,p->second);
for(multimap<int, int>::iterator k=++p;k!=map_intervals.end();k++)
{
if(k->first<=tmp.end)
tmp.end=max(tmp.end,k->second);
else
{
ret.push_back(tmp);
tmp.start=k->first;
tmp.end=k->second;
}
}
ret.push_back(tmp);
return ret;
}
};
hash table 法:Memory Limit Exceeded
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> ret;
int first=INT_MAX, last=INT_MIN;
for(vector<Interval>::iterator tmp=intervals.begin();tmp!=intervals.end();tmp++)
{
first=min((*tmp).start,first);
last=max((*tmp).end,last);
}
int count=last-first+1;
bool *a = new bool[count];
memset(a,false,sizeof(bool)*count);
for(vector<Interval>::iterator it=intervals.begin();it!=intervals.end();it++)
{
int num=(*it).end-(*it).start+1;
memset(a+(*it).start,true,sizeof(bool)*num);
}
int interval_start=0,interval_end=0;
while(interval_end<count)
{
interval_end=interval_start;
int index=0;
while(interval_start+index<count && a[index])
index++;
interval_end+=index-1;
Interval tmp_interval(interval_start,interval_end);
ret.push_back(tmp_interval);
if(interval_end<count)
{
int loc =interval_end+1;
while(loc<count && (!a[loc]))
loc++;
interval_start=loc;
}
}
}
};
leetcode || 56、 Merge Intervals的更多相关文章
- LeetCode(56)Merge Intervals
题目 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6], ...
- LeetCode OJ:Merge Intervals(合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8, ...
- 【LeetCode】Merge Intervals 题解 利用Comparator进行排序
题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- 【LeetCode】56. Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 56. Merge Intervals - LeetCode
Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...
- 刷题56. Merge Intervals
一.题目说明 题目是56. Merge Intervals,给定一列区间的集合,归并重叠区域. 二.我的做法 这个题目不难,先对intervals排序,然后取下一个集合,如果cur[0]>res ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
随机推荐
- go之数据类型转换和类型断言
一.类型转换 1.1 简单类型转换 格式 valueOfTypeB = typeB(valueOfTypeA) int 转 float64 package main import "fmt& ...
- BZOJ 3653 主席树
思路: (抄一波公式) $$ans=min(dep[x],k)×(size[x]-1)+\sum_{y在x的子树中,且dis(x,y)<=k}(size[y]-1)$$ 顺着DFS序 按照dee ...
- RabbitMQ .NET消息队列使用入门(一)【简单示例】
首先下载安装包,我都环境是win7 64位: 去官网下载 otp_win64_19.0.exe 和rabbitmq-server-3.6.3.exe安装好 然后开始编程了: (1)创建生产者类: cl ...
- SQLServer2008 在where条件中使用CASE WHEN
create table #temp( id int identity(1,1), name varchar(20), startYear int, startMonth in ...
- javascript事件绑定1-模拟jquery可爱的东西
1.给对象添加事件attachEvent(兼容IE,不兼容ff.chrome) <html xmlns="http://www.w3.org/1999/xhtml"> ...
- set statistics profile on实例
set statistics profile on实例 1.SQL语句实例 SQL 代码 复制 SET STATISTICS PROFILE ON GO SELECT COUNT(b.[Sal ...
- 【Oracle】RedHat 6.5 安装 11gR2数据库
1. 挂载操作系统光盘 [root@drz ~]# mount /dev/cdrom /mnt mount: block device /dev/sr0 is write-protected, mou ...
- Git 从github克隆文件至本地
学习阶段,同一个项目,如何保障家与公司的代码同步的问题,可以使用git克隆来解决 在家将项目提交到了GitHub上,现在来到公司,需要将GitHub上的项目克隆到本地,那么对于公司的电脑来说,同样需要 ...
- 【Shell编程】Shell基本语法
Shell 语法 Shell程序设计作为一种脚本语言,在Linux系统中有广泛的应用,本文记录了关于Shell程序设计的基础语法知识和常用命令,方便查询,熟练使用shell也需要经常实践,这对于完 ...
- Leetcode 动态规划 - 简单
1. 最大子序和 (53) 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输 ...