Compare commits
No commits in common. "95ea49c5a0fd6caa3bf394bbe81d7e1e6a70e1e6" and "69d78bd5348f858e68a3ee8e88ccf006e421565f" have entirely different histories.
95ea49c5a0
...
69d78bd534
|
|
@ -184,24 +184,4 @@ 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};
|
||||||
boolean canJump = Solution.canJump(nums);
|
Solution.removeDuplicates(nums);
|
||||||
System.out.println(canJump);
|
System.out.println(nums);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue