Compare commits

...

2 Commits

Author SHA1 Message Date
wangsiyuan 95ea49c5a0 更新 Solution.java 2023-09-13 11:49:23 +08:00
wangsiyuan cd41388601 更新 test.java 2023-09-13 11:49:21 +08:00
2 changed files with 22 additions and 2 deletions

View File

@ -184,4 +184,24 @@ public class Solution {
} }
return i; return i;
} }
/**
* nums
* true false
* @param nums
* @return
*/
public static boolean canJump(int[] nums) {
int maxReach = 0; // 初始化最远可到达的位置为0
for (int i = 0; i < nums.length; i++) {
if (i > maxReach) {
// 如果当前索引超过了最远可到达的位置,说明无法到达终点
return false;
}
// 更新最远可到达的位置
maxReach = Math.max(maxReach, i + nums[i]);
}
return true;
}
} }

View File

@ -7,8 +7,8 @@ import java.util.stream.Collectors;
public class test { public class test {
public static void main(String[] args) { public static void main(String[] args) {
int[] nums = new int[]{1,2,3,4,4,5,6}; int[] nums = new int[]{1,2,3,4,4,5,6};
Solution.removeDuplicates(nums); boolean canJump = Solution.canJump(nums);
System.out.println(nums); System.out.println(canJump);
} }
} }