提交 820a10f4 编写于 作者: Z zhangxian 提交者: labuladong

修复 Comparator 的 compare 方法里的溢出问题

在 compare 方法里,不能直接使用 a[1] - b[1],因为可能会溢出,目前这种写法在 leetcode 已经不能通过了
上级 b4a38fbe
......@@ -72,9 +72,15 @@ int intervalSchedule(int[][] intvs) {}
public int intervalSchedule(int[][] intvs) {
if (intvs.length == 0) return 0;
// 按 end 升序排序
Arrays.sort(intvs, new Comparator<int[]>() {
Arrays.sort(points, new Comparator<int[]>() {
@Override
public int compare(int[] a, int[] b) {
return a[1] - b[1];
// 这里不能使用 a[1] - b[1],要注意溢出问题
if (a[1] < b[1])
return -1;
else if (a[1] > b[1])
return 1;
else return 0;
}
});
// 至少有一个区间不相交
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册