# My code gets different answer at local from that online.

• Below is my code. for the input {{1,4},{0,4}}, i got {0,4} locally, but leetcode told my code's output was {1,4}. Can anyone tell me why? Thanks! (the main, create and print function is for debugging)

``````import java.util.*;
public class Solution {
class SortByStart implements Comparator<Interval> {
public int compare(Interval o1, Interval o2) {
if (o1.start > o2.start) return 1;
else if (o1.start < o2.start) return 0;
if (o1.end > o2.end) return 1;
else return 0;
}
}
public List<Interval> merge(List<Interval> intervals) {
List<Interval> ret = new ArrayList<Interval>();
if (intervals.size() < 1) return ret;
Collections.sort(intervals, new SortByStart());
int curs, cure;
Interval cur = intervals.get(0);
curs = cur.start;
cure = cur.end;
for (int i = 1; i < intervals.size(); ++i) {
cur = intervals.get(i);
if (cure >= cur.start) {
cure = Math.max(cur.end, cure);
} else {
Interval ni = new Interval(curs, cure);
curs = cur.start;
cure = cur.end;
}
}
return ret;
}

public List<Interval> create(int[][] x) {
List<Interval> ret = new ArrayList<Interval>();
for (int i = 0; i < x.length; ++i) {
}
return ret;
}

public void print(List<Interval> list) {
for (int i = 0; i < list.size(); ++i) {
System.out.format("{%d, %d}, ", list.get(i).start, list.get(i).end);
}
System.out.println();
}

public static void main(String[] args) {
int[][] x = {{1,4},{0,4}};
Solution s = new Solution();
s.print(s.merge(s.create(x)));
}
}``````

• Hope this would help. If you want more help, try to elaborate your thoughts.

• IMHO, why only return 0 or 1 in comprator?

In this doc, it says

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

I would say it should change to

``````class SortByStart implements Comparator<Interval> {
public int compare(Interval o1, Interval o2) {
if (o1.start > o2.start) return 1;
else if (o1.start < o2.start) return -1;
if (o1.end > o2.end) return 1;
else if (o1.end < o2.end) return -1;
return 0;
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.