@@ -331,51 +331,15 @@ You need to output 2.
331
331
public int findContentChildren(int [] g, int [] s) {
332
332
Arrays . sort(g);
333
333
Arrays . sort(s);
334
- int i = 0 , j = 0 ;
335
- while (i < g. length && j < s. length){
336
- if (g[i ] <= s[j ]) i ++ ;
337
- j ++ ;
334
+ int gIndex = 0 , sIndex = 0 ;
335
+ while (gIndex < g. length && sIndex < s. length) {
336
+ if (g[gIndex ] <= s[sIndex ]) gIndex ++ ;
337
+ sIndex ++ ;
338
338
}
339
- return i ;
339
+ return gIndex ;
340
340
}
341
341
```
342
342
343
- ** 投飞镖刺破气球**
344
-
345
- [ Leetcode : 452. Minimum Number of Arrows to Burst Balloons (Medium)] ( https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/ )
346
-
347
- ```
348
- Input:
349
- [[10,16], [2,8], [1,6], [7,12]]
350
-
351
- Output:
352
- 2
353
- ```
354
-
355
- 题目描述:气球在一个水平数轴上摆放,可以重叠,飞镖垂直射向坐标轴,使得路径上的气球都会刺破。求解最小的投飞镖次数使所有气球都被刺破。
356
-
357
- 从左往右投飞镖,并且在每次投飞镖时满足以下条件:
358
-
359
- 1 . 左边已经没有气球了;
360
- 2 . 本次投飞镖能够刺破最多的气球。
361
-
362
- ``` java
363
- public int findMinArrowShots(int [][] points) {
364
- if (points. length == 0 ) return 0 ;
365
- Arrays . sort(points,(a,b) - > (a[1 ] - b[1 ]));
366
- int curPos = points[0 ][1 ];
367
- int ret = 1 ;
368
- for (int i = 1 ; i < points. length; i++ ) {
369
- if (points[i][0 ] <= curPos) {
370
- continue ;
371
- }
372
- curPos = points[i][1 ];
373
- ret++ ;
374
- }
375
- return ret;
376
- }
377
- ```
378
-
379
343
** 股票的最大收益**
380
344
381
345
[ Leetcode : 122. Best Time to Buy and Sell Stock II (Easy)] ( https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/ )
@@ -387,8 +351,10 @@ public int findMinArrowShots(int[][] points) {
387
351
``` java
388
352
public int maxProfit(int [] prices) {
389
353
int profit = 0 ;
390
- for (int i = 1 ; i < prices. length; i++ ){
391
- if (prices[i] > prices[i- 1 ]) profit += (prices[i] - prices[i- 1 ]);
354
+ for (int i = 1 ; i < prices. length; i++ ) {
355
+ if (prices[i] > prices[i - 1 ]) {
356
+ profit += (prices[i] - prices[i - 1 ]);
357
+ }
392
358
}
393
359
return profit;
394
360
}
@@ -403,16 +369,16 @@ Input: flowerbed = [1,0,0,0,1], n = 1
403
369
Output: True
404
370
```
405
371
406
- 题目描述:花朵之间至少需要一个单位的间隔。
372
+ 题目描述:花朵之间至少需要一个单位的间隔,求解是否能种下 n 朵花 。
407
373
408
374
``` java
409
375
public boolean canPlaceFlowers(int [] flowerbed, int n) {
410
376
int cnt = 0 ;
411
- for (int i = 0 ; i < flowerbed. length; i++ ){
412
- if (flowerbed[i] == 1 ) continue ;
377
+ for (int i = 0 ; i < flowerbed. length; i++ ) {
378
+ if (flowerbed[i] == 1 ) continue ;
413
379
int pre = i == 0 ? 0 : flowerbed[i - 1 ];
414
380
int next = i == flowerbed. length - 1 ? 0 : flowerbed[i + 1 ];
415
- if (pre == 0 && next == 0 ) {
381
+ if (pre == 0 && next == 0 ) {
416
382
cnt++ ;
417
383
flowerbed[i] = 1 ;
418
384
}
@@ -433,15 +399,15 @@ Explanation: You could modify the first 4 to 1 to get a non-decreasing array.
433
399
434
400
题目描述:判断一个数组能不能只修改一个数就成为非递减数组。
435
401
436
- 在出现 nums[ i] < nums[ i - 1] 时,需要考虑的是应该修改数组的哪个数,使得本次修改能使 i 之前的数组成为非递减数组,并且 ** 不影响后续的操作** 。优先考虑令 nums[ i - 1] = nums[ i] ,因为如果修改 nums[ i] = nums[ i - 1] 的话,那么 nums[ i] 这个数会变大,那么就有可能比 nums[ i + 1] 大,从而影响了后续操作。还有一个比较特别的情况就是 nums[ i] < nums[ i - 2] ,只修改 nums[ i - 1] = nums[ i] 不能令数组成为非递减,只能通过修改 nums[ i] = nums[ i - 1] 才行。
402
+ 在出现 nums[ i] < nums[ i - 1] 时,需要考虑的是应该修改数组的哪个数,使得本次修改能使 i 之前的数组成为非递减数组,并且 ** 不影响后续的操作** 。优先考虑令 nums[ i - 1] = nums[ i] ,因为如果修改 nums[ i] = nums[ i - 1] 的话,那么 nums[ i] 这个数会变大,就有可能比 nums[ i + 1] 大,从而影响了后续操作。还有一个比较特别的情况就是 nums[ i] < nums[ i - 2] ,只修改 nums[ i - 1] = nums[ i] 不能令数组成为非递减,只能通过修改 nums[ i] = nums[ i - 1] 才行。
437
403
438
404
``` java
439
405
public boolean checkPossibility(int [] nums) {
440
406
int cnt = 0 ;
441
- for (int i = 1 ; i < nums. length; i++ ){
442
- if (nums[i] < nums[i - 1 ]){
407
+ for (int i = 1 ; i < nums. length; i++ ) {
408
+ if (nums[i] < nums[i - 1 ]) {
443
409
cnt++ ;
444
- if (i - 2 >= 0 && nums[i - 2 ] > nums[i]) nums[i] = nums[i- 1 ];
410
+ if (i - 2 >= 0 && nums[i - 2 ] > nums[i]) nums[i] = nums[i - 1 ];
445
411
else nums[i - 1 ] = nums[i];
446
412
}
447
413
}
@@ -460,15 +426,54 @@ Return true.
460
426
461
427
``` java
462
428
public boolean isSubsequence(String s, String t) {
463
- int index = 0 ;
429
+ int pos = - 1 ;
464
430
for (char c : s. toCharArray()) {
465
- index = t. indexOf(c, index );
466
- if (index == - 1 ) return false ;
431
+ pos = t. indexOf(c, pos + 1 );
432
+ if (pos == - 1 ) return false ;
467
433
}
468
434
return true ;
469
435
}
470
436
```
471
437
438
+ ** 投飞镖刺破气球**
439
+
440
+ [ Leetcode : 452. Minimum Number of Arrows to Burst Balloons (Medium)] ( https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/ )
441
+
442
+ ```
443
+ Input:
444
+ [[10,16], [2,8], [1,6], [7,12]]
445
+
446
+ Output:
447
+ 2
448
+ ```
449
+
450
+ 题目描述:气球在一个水平数轴上摆放,可以重叠,飞镖垂直投向坐标轴,使得路径上的气球都会刺破。求解最小的投飞镖次数使所有气球都被刺破。
451
+
452
+ 对气球按末尾位置进行排序,得到:
453
+
454
+ ``` html
455
+ [[1,6], [2,8], [7,12], [10,16]]
456
+ ```
457
+
458
+ 如果让飞镖投向 6 这个位置,那么 [ 1,6] 和 [ 2,8] 这两个气球都会被刺破,这种方式下刺破这两个气球的投飞镖次数最少,并且后面两个气球依然可以使用这种方式来刺破。
459
+
460
+ ``` java
461
+ public int findMinArrowShots(int [][] points) {
462
+ if (points. length == 0 ) return 0 ;
463
+ Arrays . sort(points, (a, b) - > (a[1 ] - b[1 ]));
464
+ int curPos = points[0 ][1 ];
465
+ int shots = 1 ;
466
+ for (int i = 1 ; i < points. length; i++ ) {
467
+ if (points[i][0 ] <= curPos) {
468
+ continue ;
469
+ }
470
+ curPos = points[i][1 ];
471
+ shots++ ;
472
+ }
473
+ return shots;
474
+ }
475
+ ```
476
+
472
477
** 分隔字符串使同种字符出现在一起**
473
478
474
479
[ Leetcode : 763. Partition Labels (Medium)] ( https://leetcode.com/problems/partition-labels/description/ )
0 commit comments