22* [ 1. 分配饼干] ( #1-分配饼干 )
33* [ 2. 不重叠的区间个数] ( #2-不重叠的区间个数 )
44* [ 3. 投飞镖刺破气球] ( #3-投飞镖刺破气球 )
5- * [ 3 . 根据身高和序号重组队列] ( #3 -根据身高和序号重组队列 )
6- * [ 4 . 买卖股票最大的收益] ( #4 -买卖股票最大的收益 )
7- * [ 5 . 买卖股票的最大收益 II] ( #5 -买卖股票的最大收益-ii )
8- * [ 6 . 种植花朵] ( #6 -种植花朵 )
9- * [ 7 . 判断是否为子序列] ( #7 -判断是否为子序列 )
10- * [ 8 . 修改一个数成为非递减数组] ( #8 -修改一个数成为非递减数组 )
11- * [ 9 . 子数组最大的和] ( #9 -子数组最大的和 )
12- * [ 10 . 分隔字符串使同种字符出现在一起] ( #10 -分隔字符串使同种字符出现在一起 )
5+ * [ 4 . 根据身高和序号重组队列] ( #4 -根据身高和序号重组队列 )
6+ * [ 5 . 买卖股票最大的收益] ( #5 -买卖股票最大的收益 )
7+ * [ 6 . 买卖股票的最大收益 II] ( #6 -买卖股票的最大收益-ii )
8+ * [ 7 . 种植花朵] ( #7 -种植花朵 )
9+ * [ 8 . 判断是否为子序列] ( #8 -判断是否为子序列 )
10+ * [ 9 . 修改一个数成为非递减数组] ( #9 -修改一个数成为非递减数组 )
11+ * [ 10 . 子数组最大的和] ( #10 -子数组最大的和 )
12+ * [ 11 . 分隔字符串使同种字符出现在一起] ( #11 -分隔字符串使同种字符出现在一起 )
1313<!-- GFM-TOC -->
1414
1515
@@ -78,18 +78,18 @@ Explanation: You don't need to remove any of the intervals since they're already
7878按区间的结尾进行排序,每次选择结尾最小,并且和前一个区间不重叠的区间。
7979
8080``` java
81- public int eraseOverlapIntervals(Interval [] intervals) {
81+ public int eraseOverlapIntervals(int [] [] intervals) {
8282 if (intervals. length == 0 ) {
8383 return 0 ;
8484 }
85- Arrays . sort(intervals, Comparator . comparingInt(o - > o. end ));
85+ Arrays . sort(intervals, Comparator . comparingInt(o - > o[ 1 ] ));
8686 int cnt = 1 ;
87- int end = intervals[0 ]. end ;
87+ int end = intervals[0 ][ 1 ] ;
8888 for (int i = 1 ; i < intervals. length; i++ ) {
89- if (intervals[i]. start < end) {
89+ if (intervals[i][ 0 ] < end) {
9090 continue ;
9191 }
92- end = intervals[i]. end ;
92+ end = intervals[i][ 1 ] ;
9393 cnt++ ;
9494 }
9595 return intervals. length - cnt;
@@ -99,10 +99,10 @@ public int eraseOverlapIntervals(Interval[] intervals) {
9999使用 lambda 表示式创建 Comparator 会导致算法运行时间过长,如果注重运行时间,可以修改为普通创建 Comparator 语句:
100100
101101``` java
102- Arrays . sort(intervals, new Comparator<Interval > () {
102+ Arrays . sort(intervals, new Comparator<int[] > () {
103103 @Override
104- public int compare (Interval o1 , Interval o2 ) {
105- return o1. end - o2. end ;
104+ public int compare (int [] o1 , int [] o2 ) {
105+ return o1[ 1 ] - o2[ 1 ] ;
106106 }
107107});
108108```
@@ -141,7 +141,7 @@ public int findMinArrowShots(int[][] points) {
141141}
142142```
143143
144- # 3 . 根据身高和序号重组队列
144+ # 4 . 根据身高和序号重组队列
145145
146146[ 406. Queue Reconstruction by Height(Medium)] ( https://leetcode.com/problems/queue-reconstruction-by-height/description/ )
147147
@@ -173,7 +173,7 @@ public int[][] reconstructQueue(int[][] people) {
173173}
174174```
175175
176- # 4 . 买卖股票最大的收益
176+ # 5 . 买卖股票最大的收益
177177
178178[ 121. Best Time to Buy and Sell Stock (Easy)] ( https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/ )
179179
@@ -196,7 +196,7 @@ public int maxProfit(int[] prices) {
196196```
197197
198198
199- # 5 . 买卖股票的最大收益 II
199+ # 6 . 买卖股票的最大收益 II
200200
201201[ 122. Best Time to Buy and Sell Stock II (Easy)] ( https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/ )
202202
@@ -217,7 +217,7 @@ public int maxProfit(int[] prices) {
217217```
218218
219219
220- # 6 . 种植花朵
220+ # 7 . 种植花朵
221221
222222[ 605. Can Place Flowers (Easy)] ( https://leetcode.com/problems/can-place-flowers/description/ )
223223
@@ -247,7 +247,7 @@ public boolean canPlaceFlowers(int[] flowerbed, int n) {
247247}
248248```
249249
250- # 7 . 判断是否为子序列
250+ # 8 . 判断是否为子序列
251251
252252[ 392. Is Subsequence (Medium)] ( https://leetcode.com/problems/is-subsequence/description/ )
253253
@@ -269,7 +269,7 @@ public boolean isSubsequence(String s, String t) {
269269}
270270```
271271
272- # 8 . 修改一个数成为非递减数组
272+ # 9 . 修改一个数成为非递减数组
273273
274274[ 665. Non-decreasing Array (Easy)] ( https://leetcode.com/problems/non-decreasing-array/description/ )
275275
@@ -303,7 +303,7 @@ public boolean checkPossibility(int[] nums) {
303303
304304
305305
306- # 9 . 子数组最大的和
306+ # 10 . 子数组最大的和
307307
308308[ 53. Maximum Subarray (Easy)] ( https://leetcode.com/problems/maximum-subarray/description/ )
309309
@@ -327,7 +327,7 @@ public int maxSubArray(int[] nums) {
327327}
328328```
329329
330- # 10 . 分隔字符串使同种字符出现在一起
330+ # 11 . 分隔字符串使同种字符出现在一起
331331
332332[ 763. Partition Labels (Medium)] ( https://leetcode.com/problems/partition-labels/description/ )
333333
0 commit comments