Skip to content

Commit 2e1f8c4

Browse files
committed
add leetcode: 55,56
1 parent f8d0fff commit 2e1f8c4

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

Algorithm/src/leetCode/subject/number51_100/_54LeetCode.java Algorithm/src/leetCode/subject/number51_100/_54SpiralMatrix.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
/**
77
* @author : CodeWater
88
* @create :2022-04-25-13:19
9-
* @Function Description :螺旋矩阵
9+
* @Function Description :54.螺旋矩阵
1010
* https://leetcode-cn.com/problems/spiral-matrix/
1111
*/
12-
public class _54LeetCode {
12+
public class _54SpiralMatrix {
1313
class Solution {
1414
public List<Integer> spiralOrder(int[][] matrix) {
1515
List<Integer> ans = new ArrayList<>();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

0 commit comments

Comments
 (0)