Merge Interval leetcode java

题目:

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].

题解:

这道题主要难点是改写Comparator。

Comparator接口定义了两个方法:compare( )和equals( )。这里给出的compare( )方法按顺序比较了两个元素:
  
  int compare(Object obj1, Object obj2)
  
 
 obj1和obj2是被比较的两个对象。当两个对象相等时,该方法返回0;当obj1大于obj2时,返回一个正值;否则,返回一个负值。如果用于比较
的对象的类型不兼容的话,该方法引发一个ClassCastException异常。通过覆盖compare(
),可以改变对象排序的方式。例如,通过创建一个颠倒比较输出的比较函数,可以实现按逆向排序。
  
  这里给出的equals( )方法,测试一个对象是否与调用比较函数相等:
  
  boolean equals(Object obj)
  
  obj是被用来进行相等测试的对象。如果obj和调用对象都是Comparator的对象并且使用相同的排序。该方法返回true.否则返回false.重载equals( )方法是没有必要的,大多数简单的比较函数都不这样做。

具体代码如下:

1     public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
 2         if (intervals == null || intervals.size() <= 1)
 3             return intervals;
 4  
 5         // sort intervals by using self-defined Comparator
 6         Collections.sort(intervals, new IntervalComparator());
 7  
 8         ArrayList<Interval> result = new ArrayList<Interval>();
 9  
10         Interval prev = intervals.get(0);
11         for (int i = 1; i < intervals.size(); i++) {
12             Interval curr = intervals.get(i);
13  
14             if (prev.end >= curr.start) {
15                 // merged case
16                 Interval merged = new Interval(prev.start, Math.max(prev.end, curr.end));
17                 prev = merged;
18             } else {
19                 result.add(prev);
20                 prev = curr;
21             }
22         }
23  
24         result.add(prev);
25  
26         return result;
27         }
28     }
29  
30     class IntervalComparator implements Comparator<Interval> {
31         public int compare(Interval i1, Interval i2) {
32             return i1.start - i2.start;
33         }

Refrence:

http://www.programcreek.com/2012/12/leetcode-merge-intervals/

http://www.blogjava.net/yesjoy/articles/126046.html

Merge Interval leetcode java

时间: 2024-11-07 12:51:53

Merge Interval leetcode java的相关文章

Insert Interval leetcode java

题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and me

Merge Two Sorted Lists leetcode java

题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 题解: 这道题是链表操作题,题解方法很直观. 首先,进行边界条件判断,如果任一一个表是空表,就返回另外一个表. 然后,对于新表选取第一个node,选择两个表表头最小的那个作为新表表头,指针后挪. 然后同时遍历

Merge k Sorted Lists leetcode java

题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解: Merge k sorted linked list就是merge 2 sorted linked list的变形题. 而且我们很自然的就想到了经典的Merge Sort,只不过那个是对数组进行sort.而不同的地方,仅仅是Merge两个list的操作不同. 这里来复习一下Merge

Merge Sorted Array leetcode java(回顾MergeTwoArray和MergeTwoLinkedList)

题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B a

Sort List leetcode java

题目: Sort a linked list in O(n log n) time using constant space complexity. 题解: 考虑到要求用O(nlogn)的时间复杂度和constant space complexity来sort list,自然而然想到了merge sort方法.同时我们还已经做过了merge k sorted list和merge 2 sorted list.这样这个问题就比较容易了. 不过这道题要找linkedlist中点,那当然就要用最经典的

Spiral Matrix leetcode java

题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 题解: 这道题是实现题. 考虑2个初始

Pascal&#39;s Triangle II Leetcode java

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 题解: 为了达到O(k)的空间复杂度要求,那么就要从右向左生成结果.相当于你提前把上一行的计算出来,当前行就可以用上一次计算出的结果计算了

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

Pascal&#39;s Triangle leetcode java(杨辉三角)

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题解:既然讲到了Pascal‘s Triangle,即杨辉三角.那么就先去Wikipedia上面复习一下杨辉三角吧:”杨辉三角形,又称賈憲三角形.帕斯卡三角形.海亚姆三角形,是二项式係數在的