13
import java.util.*;
public class Solution {
public ArrayList merge(ArrayList intervals) {
Collections.sort(intervals, new Comparator() {
public int compare(Interval o1, Interval o2) {
return o1.start-o2.start;
}
});
for (int i = 1; i < intervals.size(); i++) {
int preStart = intervals.get(i - 1).start;
int preEnd = intervals.get(i - 1).end;
int curStart = intervals.get(i).start;
int curEnd = intervals.get(i).end;
if (curStart <= preEnd) {
intervals.set(i, new Interval(preStart, Math.max(preEnd, curEnd)));
intervals.set(i - 1, null);
}
}
while (intervals.remove(null)) ;
return intervals;
}
}
发表于 2017-11-22 18:47:42
回复(1)
8
class Solution {
public:
vector merge(vector &intervals) {
vector result;
int l = intervals.size();
if(l==0)
return result;
sort(intervals.begin(), intervals.end(), cmp);
result.push_back(intervals[0]);
for(int i=1;i
{
if(result.back().end >= intervals[i].start)
result.back().end = max(result.back().end, intervals[i].end);
else
result.push_back(intervals[i]); } return result;
}
static bool cmp(const Interval &a, const Interval &b)
{
return a.start < b.start; }
};
发表于 2017-09-27 07:47:43
回复(1)
3
import java.util.*;
public class Solution {
public ArrayList merge(ArrayList intervals) {
ArrayList res=new ArrayList();
if(intervals.size()==0||intervals==null)
return res;
Collections.sort(intervals, (a,b) -> a.start - b.start);
for(int i=1;i
{
if(intervals.get(i).start<=intervals.get(i-1).end)
{
intervals.get(i).start=intervals.get(i-1).start;
intervals.get(i).end=Math.max(intervals.get(i).end,intervals.get(i-1).end);
}
else
res.add(intervals.get(i-1));
}
res.add(intervals.get(intervals.size()-1));
return res;
}
}
编辑于 2020-01-23 19:21:24
回复(0)
3
class Solution {
public:
// 先按照start从小到大时间轴排序,有重叠部分则进行合并
static bool compare(const Interval &a,const Interval &b)
{
return (a.start < b.start);
}
vector merge(vector &intervals)
{
vector res;
int len = intervals.size();
if(len==0) return res;
sort(intervals.begin(),intervals.end(),compare);
res.push_back(intervals[0]);
for(int i=1;i
{
if(res.back().end >= intervals[i].start)
res.back().end = max(res.back().end,intervals[i].end);
else
res.push_back(intervals[i]);
}
return res;
}
};
发表于 2017-07-08 15:53:53
回复(0)
2
Comparator cmp = new Comparator() {
@Override
public int compare(Interval o1, Interval o2) {
// TODO Auto-generated method stub
if(o1.start > o2.start) return 1;
else return -1;
}
};
public ArrayList merge(ArrayList intervals) {
if(intervals.size()<= 1) return intervals;
ArrayList result = new ArrayList();
intervals.sort(cmp);
int start = intervals.get(0).start;
int end = intervals.get(0).end;
for(int i=1; i
Interval interval = intervals.get(i);
if(interval.start>end){
result.add(new Interval(start,end));
start = interval.start;
end = interval.end;
continue;
}
if(interval.start <= start && start<=interval.end) start = interval.start;
if(interval.start <= end && end <= interval.end) end = interval.end;
}
result.add(new Interval(start, end));
return result;
}
发表于 2018-07-28 11:40:12
回复(0)
2
//merge intervals
/* 思路:先对intervals进行排序,然后对intervals进行两两对比
class Solution
{
public:
static bool compare(const Interval &a,const Interval &b) //仅本作用域可见
{
return (a.start
}
vector merge(vector &intervals)
{
vector res;
if(intervals.empty()) return res;
sort(intervals.begin(),intervals.end(),compare);
res.push_back(intervals[0]);
//已经排好序了,只需要进行两两的比较即可
for(int i=1;i
{
if(res.back().end>=intervals[i].start)
{
res.back().end=max(res.back().end,intervals[i].end);
}
else
{
res.push_back(intervals[i]);
}
}
return res;
}
};
发表于 2017-04-11 09:58:45
回复(2)
1
想请教一下各位,我这个代码有啥地方没有考虑到呀?为什么提示是“格式错误:您的程序输出的格式不符合要求(比如空格和换行与要求不一致)”,对比着评论看了还是摸不着头脑......
import java.util.*;
public class Solution {
public ArrayList merge(ArrayList intervals) {
ArrayList ret = new ArrayList<>();
if(intervals==null || intervals.size()==0)
return ret;
intervals.sort((a, b) -> a.start - b.start);
int prevStart = intervals.get(0).start;
int prevEnd = intervals.get(0).end;
for (int i = 1; i
Interval currInterval = intervals.get(i);
if (currInterval.start <= prevEnd) {
prevEnd = Math.max(currInterval.end, prevEnd);
} else {
// Save prev combined interval
Interval temp = new Interval(prevStart, prevEnd);
ret.add(temp);
prevStart = currInterval.start;
prevEnd = currInterval.end;
if (i + 1 == intervals.size()) {
Interval lastTemp = new Interval(prevStart, prevEnd);
ret.add(lastTemp);
}
}
}
return ret;
}
} 先谢谢了!!祝各位offer多多
发表于 2020-10-08 21:26:56
回复(0)
1
和大部分人的代码不一样,没有定义两个类,思路却容易理解
class Solution:
def merge(self , intervals ):
intervals.sort()#按第一个数字排序以防输入的区间顺序错乱
list1=[]
while len(intervals)>1:#为了使后续语句intervals[1][0]不出现越界错误,故设置长度大于1
if intervals[0][1]
list1.append(intervals[0])
intervals.pop(0)#添加完成删除该区间
else:#否则两个区间有交集
intervals[0]=[min(intervals[0][0],intervals[1][0]),max(intervals[0][1],intervals[1][1])]#取两个区间的下界的最小值作为合并后区间的下界,取两个区间的上界的最大值作为合并后区间的上界,即完成两个区间的并集
intervals.pop(1)#合并后intervals的第一个元素用合并后的区间替换,那么参与合并的第二个元素(下标为1)也应该要删除
list1.append(intervals[0])#intervals还剩一个元素即退出循环体,故剩下的一个元素需要添加进来
list1=map(str,list1)#将列表元素的数据类型由整型转化为字符串,才能使用下面的join语句,否则报错
return ",".join(list1)#返回列表的所有元素,即去掉中括号后的列表
#print(Solution().merge([[18,18],[1,2],[8,10],[4,11],[0,8],[19,22]]))
发表于 2020-09-19 18:13:08
回复(0)
1
/**
* 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 merge(vector &intervals) {
if(intervals.size() <= 1)
return intervals;
vector vec;
sort(intervals.begin(),intervals.end(),[](Interval a,Interval b){return a.start
Interval tmp = intervals[0];
for(int i = 1; i
{
if(tmp.end
{
vec.push_back(tmp);
tmp = intervals[i];
}
else
tmp.end = max(intervals[i].end,tmp.end);
}
vec.push_back(tmp);
return vec;
}
};
发表于 2019-12-19 15:03:53
回复(0)
1
思路
1.先将Interval数组按照start、end从小到大进行排序;
2.设置指针i,从头遍历到intervals.size() - 1,当intervals.get(i).end >= intervals.get(i).start时候,将这两个元素进行合并,然后更新最大的end,以便接下来继续遍历比较;
3.如果结束循环的时候,i == intervals.size() - 1,那么需要将intervals.get(i)加入到结果集合中。 import java.util.ArrayList;
import java.util.Collections;
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public ArrayList merge(ArrayList intervals) {
ArrayList res = new ArrayList<>();
Collections.sort(intervals, (o1, o2) -> o1.start != o2.start ? o1.start - o2.start : o1.end - o2.end);
int i = 0;
while (i < intervals.size() - 1) {
int left = intervals.get(i).start;
int right = intervals.get(i).end;
while (i < intervals.size() - 1 && intervals.get(i + 1).start <= right) {
right = Math.max(right, intervals.get(i + 1).end);
i++;
}
res.add(new Interval(left, right));
i++;
}
if (i == intervals.size() - 1) {
res.add(intervals.get(i));
}
return res;
}
}
发表于 2019-11-04 17:16:05
回复(1)
1
//按start进行排序,然后一个个的加到结果res中。
//如果待加入节点的start <= res.back().end 说明相交,直接更新end,取节点end和当res.back().end中的较大值。
//如果start > res.back()则不相交 直接加入res中。
//第一个节点,也直接加入res中
vector merge(vector &intervals)
{
sort(intervals.begin(), intervals.end(),[](Interval x, Interval y){return x.start
vector res;
for (int i = 0; i
{
if (i == 0 || intervals[i].start > res.back().end)
{
res.push_back(intervals[i]);
}
else
{
res.back().end = max(intervals[i].end, res.back().end);
}
}
return res;
}
发表于 2019-08-20 11:11:53
回复(0)
1
import java.util.*;
public class Solution {
public ArrayList merge(ArrayList intervals) {
ArrayList res = new ArrayList<>();
if(intervals.size() == 0 || intervals == null) return res;
// sort需要实现compare接口,不是comparable接口
Collections.sort(intervals, (a,b) -> a.start - b.start);
int newStart = intervals.get(0).start;
int newEnd = intervals.get(0).end;
for(Interval tmp : intervals){
if(tmp.start <= newEnd && tmp.end >= newEnd) newEnd = tmp.end; //排好序后不用比较tmp.start和newStart
if(tmp.start > newEnd) {
res.add(new Interval(newStart, newEnd));
newStart = tmp.start;
newEnd = tmp.end;
}
}
res.add(new Interval(newStart, newEnd));
return res;
}
}
发表于 2019-01-29 14:49:42
回复(1)
1
/**
* 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 merge(vector &intervals) {
int s=intervals.size();
if(s==0) return intervals;
for(int i=0;i
int m=intervals[i].start;
int tmp=i;
for(int j=i;j
if(intervals[j].start
m=intervals[j].start;
tmp=j;
}
}
int temp=intervals[i].start;
intervals[i].start=intervals[tmp].start;
intervals[tmp].start=temp;
temp=intervals[i].end;
intervals[i].end=intervals[tmp].end;
intervals[tmp].end=temp;
}
for(int i=0;i
if(intervals[i].start==intervals[i+1].start){
intervals.erase(intervals.begin()+(intervals[i].end>intervals[i+1].end?i+1:i));
--i;
}
else if(intervals[i].end>=intervals[i+1].start){
intervals[i].end=(intervals[i].end>intervals[i+1].end?intervals[i].end:intervals[i+1].end);
intervals.erase(intervals.begin()+i+1);
--i;
}
}
return intervals;
}
};
发表于 2018-08-09 15:37:47
回复(0)
2
#include
bool comp(const Interval &in1,const Interval &in2){
if(in1.start==in2.start){
return in1.end
}else{
return in1.start
}
}
class Solution {
public:
vector merge(vector &intervals) {
sort(intervals.begin(),intervals.end(),comp); //按start升序排序
vector res;
int len = intervals.size();
for(int i=0;i
if(res.empty()){
res.push_back(intervals[i]);
}else{
Interval last = res.back();
if(last.end>=intervals[i].start){ //需要进行合并
res.pop_back();
last.end = max(last.end,intervals[i].end);
res.push_back(last);
}else{
res.push_back(intervals[i]);
}
}
}
return res;
}
};
编辑于 2016-06-25 19:13:46
回复(0)
1
/**
* 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 merge(vector& intervals) {
vector v;
int n = intervals.size();
if (0 == n) {
return v;
}
if (1 == n) {
v.push_back(intervals[0]);
return v;
}
sort(intervals.begin(),intervals.end(), compare_Interval);
Interval temp = intervals[0];
int i = 1;
while (i < n) {
if (temp.end < intervals[i].start) {
v.push_back(temp);
temp = intervals[i];
++i;
if (i == n) {
v.push_back(temp);
}
}
else {
temp.start = min(temp.start, intervals[i].start);
temp.end = max(temp.end, intervals[i].end);
++i;
if (i == n) {
v.push_back(temp);
}
}
}
return v;
}
static int compare_Interval(Interval val1, Interval val2){
return val1.start < val2.start;
}
};
编辑于 2017-11-29 22:15:05
回复(0)
1
static bool comp(const Interval& a,const Interval& b){
return (a.start<= b.start);
}
vector merge(vector &intervals) {
if(intervals.size()==0)
return intervals;
sort(intervals.begin(),intervals.end(),comp);
vector res;
res.push_back(intervals[0]);
for(int i=1;i
if(intervals[i].start>res.back().end)
res.push_back(intervals[i]);
else{
res.back().end=std::max(intervals[i].end,res.back().end);
}
}
return res;
}
发表于 2017-11-06 15:53:36
回复(0)
1
Python最简洁代码: class Solution(object):
def merge(self, l):
l.sort(key=lambda x: x.start)
i = 0
while i < len(l) - 1:
if l[i].end >= l[i + 1].start:
l[i].end = max(l[i].end, l[i + 1].end)
l.pop(i + 1)
else:
i += 1
return l
发表于 2017-10-07 11:18:52
回复(1)
1
import java.util.*;
public class Solution {
public ArrayList merge(ArrayList intervals) {
if(intervals.size() < 2) return intervals;
Collections.sort(intervals, new Comparator() {
@Override
public int compare(Interval o1, Interval o2) {
if(o1.start == o2.start) return o1.end < o2.end ? - 1 : 1;
return o1.start < o2.start ? - 1 : 1;
}
});
ArrayList list = new ArrayList<>();
list.add(intervals.get(0));
for (int i = 1; i < intervals.size(); i ++) {
Interval cur = intervals.get(i);
Interval ready = list.get(list.size() - 1);
if(cur.start <= ready.end) {
ready.end = cur.end > ready.end ? cur.end : ready.end;
list.remove(list.size() - 1);
list.add(ready);
} else list.add(cur);
}
return list;
}
}
发表于 2017-03-25 16:25:38
回复(0)
1
// 想法很简单,先排个序,按照start的升序,然后依次和后面的interval比较有没有交集
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Solution {
public ArrayList merge(ArrayList intervals) {
ArrayList list = new ArrayList<>();
if(intervals == null)
return list;
if(intervals.size() <= 1)
return intervals;
Collections.sort(intervals, new Comparator() {
public int compare(Interval int1, Interval int2){
return int1.start - int2.start;
}
});
Interval cur = intervals.get(0);
for(int i = 1; i < intervals.size(); i++){
if(cur.end >= intervals.get(i).start){
cur.start = Math.min(cur.start, intervals.get(i).start);
cur.end = Math.max(cur.end, intervals.get(i).end);
if(i == intervals.size() - 1)
list.add(cur);
}
else{
list.add(cur);
cur = intervals.get(i);
if(i == intervals.size() - 1)
list.add(cur);
}
}
return list;
}
}
发表于 2017-05-15 14:51:03
回复(0)
0
排序+贪心
import java.util.*;
public class Solution {
public ArrayList merge(ArrayList intervals) {
ArrayList res = new ArrayList<>();
if (intervals.size() == 0) {
return res;
}
Collections.sort(intervals,
new Comparator() {
public int compare(Interval a, Interval b) {
return a.start - b.start;
}
});
res.add(intervals.get(0));
for (int i = 1;i
int preStart = res.get(res.size()-1).start;
int preEnd = res.get(res.size()-1).end;
int curStart = intervals.get(i).start;
int curEnd = intervals.get(i).end;
if (preEnd >= curStart) {
res.set(res.size()-1,
new Interval(preStart, Math.max(curEnd, preEnd)));
}else {
res.add(intervals.get(i));
}
}
return res;
}
}
发表于 2021-01-10 10:03:05
回复(0)