Compare commits
2 Commits
69d78bd534
...
95ea49c5a0
| Author | SHA1 | Date |
|---|---|---|
|
|
95ea49c5a0 | |
|
|
cd41388601 |
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue