File tree 3 files changed +65
-2
lines changed
Algorithm/src/leetCode/subject/number51_100
3 files changed +65
-2
lines changed Original file line number Diff line number Diff line change 6
6
/**
7
7
* @author : CodeWater
8
8
* @create :2022-04-25-13:19
9
- * @Function Description :螺旋矩阵
9
+ * @Function Description :54. 螺旋矩阵
10
10
* https://leetcode-cn.com/problems/spiral-matrix/
11
11
*/
12
- public class _54LeetCode {
12
+ public class _54SpiralMatrix {
13
13
class Solution {
14
14
public List <Integer > spiralOrder (int [][] matrix ) {
15
15
List <Integer > ans = new ArrayList <>();
Original file line number Diff line number Diff line change
1
+ package leetCode .subject .number51_100 ;
2
+
3
+ /**
4
+ * @author : CodeWater
5
+ * @create :2022-08-03-16:46
6
+ * @Function Description :55.跳跃游戏
7
+ */
8
+ public class _55JumpingGame {
9
+ class Solution {
10
+ /**跳跃的步数可以选择在数组元素的值以内,不一定每一步就跳定长!
11
+ */
12
+ public boolean canJump (int [] nums ) {
13
+ // j表示当前可以跳的最远距离
14
+ for ( int i = 0 , j = 0 ; i < nums .length ; i ++ ){
15
+ // 如果j不能大于等于i,说明无法到达该位置,那么也就无法到达最后
16
+ if ( j < i ) return false ;
17
+ // j能都到达的位置,取最大值
18
+ j = Math .max ( j , i + nums [i ] );
19
+ }
20
+ // 遍历结束都没有return false,说明最后j还是大于等于i的,可以到达
21
+ return true ;
22
+ }
23
+ }
24
+ }
Original file line number Diff line number Diff line change
1
+ package leetCode .subject .number51_100 ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .Arrays ;
5
+ import java .util .List ;
6
+
7
+ /**
8
+ * @author : CodeWater
9
+ * @create :2022-08-03-17:13
10
+ * @Function Description :56. 合并区间
11
+ */
12
+ public class _56MergeRange {
13
+ class Solution {
14
+ /**模板题:
15
+ 1. 按照左端点排序
16
+ 2. 当前区间的左端点和上一个区间有交集,那就更新右端点
17
+ 3. 没有交集,当前区间无需合并,保存当前区间
18
+ */
19
+ public int [][] merge (int [][] arr ) {
20
+ Arrays .sort ( arr , (o1 , o2 ) -> o1 [0 ] - o2 [0 ]);
21
+ List <int []> list = new ArrayList <>();
22
+ int l = arr [0 ][0 ] , r = arr [0 ][1 ];
23
+
24
+ for ( int i = 1 ; i < arr .length ; i ++ ){
25
+ if ( arr [i ][0 ] > r ){
26
+ list .add ( new int []{l , r } );
27
+ l = arr [i ][0 ];
28
+ r = arr [i ][1 ];
29
+ }else {
30
+ r = Math .max ( arr [i ][1 ] , r );
31
+ }
32
+ }
33
+ // 加上最后一个区间
34
+ list .add ( new int []{l , r } );
35
+
36
+ return list .toArray ( new int [list .size ()][2 ] );
37
+ }
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments