Leetcode#56Merge Intervals

Merge Intervals

Total Accepted: 34742 Total Submissions: 155846My Submissions

Question Solution

Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

public class Solution {

TreeNode root;

List<Interval> z=new ArrayList<Interval>();

void InsertNode(Interval y){

TreeNode cur=root;

while(cur!=null)

{

int cur_s=cur.val.start;

int cur_e=cur.val.end;

if(cur_s>y.end)

{

if(cur.left==null)

{

TreeNode newnode=new TreeNode(y);

cur.left=newnode;

newnode.parent=cur;

break;

}

else

{

cur=cur.left;

}

}

else if(cur_e<y.start)

{

if(cur.right==null)

{

TreeNode newnode=new TreeNode(y);

cur.right=newnode;

newnode.parent=cur;

break;

}

else

{

cur=cur.right;

}

}

else

{

if(cur_s<=y.start&&cur_e>=y.end)

break;

else if(cur_s<=y.start&&cur_e<y.end)

{

//---------

//-----

cur.val.end=y.end;

while(cur.right!=null)

{

TreeNode p=cur.right;

while(p.left!=null)

{

p=p.left;

}

if(cur.val.end<p.val.start)

break;

else

{

if(p.parent.left==p)

{

p.parent.left=p.right;

if(p.right!=null)

p.right.parent=p.parent;

}

else

{

p.parent.right=p.right;

if(p.right!=null)

p.right.parent=p.parent;

}

if(p.val.end>cur.val.end)

cur.val.end=p.val.end;

}

}

break;

}

else if(cur_s>y.start&&cur_e>=y.end)

{

cur.val.start=y.start;

while(cur.left!=null)

{

TreeNode p=cur.left;

while(p.right!=null)

{

p=p.right;

}

if(cur.val.start>p.val.end)

break;

else

{

if(p.parent.left==p)

{

p.parent.left=p.left;

if(p.left!=null)

p.left.parent=p.parent;

}

else

{

p.parent.right=p.left;

if(p.left!=null)

p.left.parent=p.parent;

}

if(p.val.start<cur.val.start)

cur.val.start=p.val.start;

}

}

break;

}

else

{

cur.val.end=y.end;

while(cur.right!=null)

{

TreeNode p=cur.right;

while(p.left!=null)

{

p=p.left;

}

if(cur.val.end<p.val.start)

break;

else

{

if(p.parent.left==p)

{

p.parent.left=p.right;

if(p.right!=null)

p.right.parent=p.parent;

}

else

{

p.parent.right=p.right;

if(p.right!=null)

p.right.parent=p.parent;

}

if(p.val.end>cur.val.end)

cur.val.end=p.val.end;

}

}

cur.val.start=y.start;

while(cur.left!=null)

{

TreeNode p=cur.left;

while(p.right!=null)

{

p=p.right;

}

if(cur.val.start>p.val.end)

break;

else

{

if(p.parent.left==p)

{

p.parent.left=p.left;

if(p.left!=null)

p.left.parent=p.parent;

}

else

{

p.parent.right=p.left;

if(p.left!=null)

p.left.parent=p.parent;

}

if(p.val.start<cur.val.start)

cur.val.start=p.val.start;

}

}

break;

}

}

}

}

void GenerateTree(List<Interval> t){

for(int i=1;i<t.size();i++)

{

InsertNode(t.get(i));

}

}

void TravelTree(TreeNode h)

{

if(h!=null)

{

z.add(h.val);

TravelTree(h.left);

TravelTree(h.right);

}

}

public List<Interval> merge(List<Interval> intervals) {

int s=intervals.size();

if(s==0)

return z;

else if(s==1)

{

z.add(intervals.get(0));

return z;

}

else

{

root=new TreeNode(intervals.get(0));

GenerateTree(intervals);

TravelTree(root);

return z;

}

}

}

class TreeNode{

Interval val;

TreeNode left;

TreeNode right;

TreeNode parent;

TreeNode(Interval j){

val=j;

parent=null;

left=null;

right=null;

}

}

时间: 2024-11-10 03:07:46

Leetcode#56Merge Intervals的相关文章

LeetCode: Merge Intervals [055]

[题目] Given an array of non-negative integers, you are initially positioned at the first index of the array. Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].

[leetcode]Merge Intervals @ Python

原题地址:https://oj.leetcode.com/problems/merge-intervals/ 题意: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 解题思路:先将区间按照每个start的值来排序,排好序以后判断一个区间的start值是否处在前一个区间

2015.03.30 LeetCode Merge Intervals 解题记录

今天下午做了一道题.leetcode merge intervals 属于比较难的题目. 首先用collections.sort 给list排序,然后用两个while loop来比较两个interval 的start, end . 从而生成新的interal,再插入到新的list 返回结果. 下面给出自己的代码: /* 50 Merge Intervals https://leetcode.com/problems/merge-intervals/ Given a collection of i

LeetCode Merge Intervals

原题链接在这里:https://leetcode.com/problems/merge-intervals/ 首先sort list 中的interval. 然后比较 前一个 interval的end是否 >= 后一个interval 的start, 若是,则合并这两个interval, 用来和下一个比较.若不是,则把这段interval 加到res中去. 通过本题见到了如何改写Comparator, 参见了这篇文章:http://www.blogjava.net/yesjoy/articles

LeetCode: Merge Intervals 解题报告

Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. SOLUTION 1: 1. 先使用Comparator 的匿名类对intervels进行排序. 2. 把Intervals遍历一次,依次一个一个merge到第1个interval. 把第1

[LeetCode] Merge Intervals 排序sort

Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. Show Tags Array Sort 这题其实想好思路很好解决,对于框,如果下个框开始在 其中间,则连在一起,否则单独为一个,这需要按start 排序便可以了,因为类中写自定义比较函数比较麻烦,所以一次写了好几个.

Leetcode: Non-overlapping Intervals

Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Note: You may assume the interval's end point is always bigger than its start point. Intervals like [1,2] and

LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。

感觉有一点进步了,但是思路还是不够犀利. /** * 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> merge(vector<In

[Leetcode] merge intervals 合并区间

Given a collection of intervals, merge all overlapping intervals. For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18]. 题意:给定一系列区间,合并重叠区间 思路:其实,个人觉得,题目的例子有一定误导性,以为区间已经按每个区间的start值排好序了.结果,毫无疑问,悲剧了.若是已经排好序,则,只需将当前区间的end值和下一个的start值比较,