diff --git a/docs/notes/Leetcode 题解 - 贪心思想.md b/docs/notes/Leetcode 题解 - 贪心思想.md index f75bddc9..87669d52 100644 --- a/docs/notes/Leetcode 题解 - 贪心思想.md +++ b/docs/notes/Leetcode 题解 - 贪心思想.md @@ -106,13 +106,15 @@ public int eraseOverlapIntervals(int[][] intervals) { ```java Arrays.sort(intervals, new Comparator() { - @Override - public int compare(int[] o1, int[] o2) { - return o1[1] - o2[1]; - } + @Override + public int compare(int[] o1, int[] o2) { + return (o1[1] < o2[1]) ? -1 : ((o1[1] == o2[1]) ? 0 : 1); + } }); ``` +实现 compare() 函数时避免使用 `return o1[1] - o2[1];` 这种减法操作,防止溢出。 + # 3. 投飞镖刺破气球 452\. Minimum Number of Arrows to Burst Balloons (Medium) diff --git a/notes/Leetcode 题解 - 贪心思想.md b/notes/Leetcode 题解 - 贪心思想.md index f75bddc9..87669d52 100644 --- a/notes/Leetcode 题解 - 贪心思想.md +++ b/notes/Leetcode 题解 - 贪心思想.md @@ -106,13 +106,15 @@ public int eraseOverlapIntervals(int[][] intervals) { ```java Arrays.sort(intervals, new Comparator() { - @Override - public int compare(int[] o1, int[] o2) { - return o1[1] - o2[1]; - } + @Override + public int compare(int[] o1, int[] o2) { + return (o1[1] < o2[1]) ? -1 : ((o1[1] == o2[1]) ? 0 : 1); + } }); ``` +实现 compare() 函数时避免使用 `return o1[1] - o2[1];` 这种减法操作,防止溢出。 + # 3. 投飞镖刺破气球 452\. Minimum Number of Arrows to Burst Balloons (Medium)