55. 跳跃游戏

跳跃游戏 - 图1

  1. class Solution {
  2. public boolean canJump(int[] nums) {
  3. int n = nums.length;
  4. int farthest = 0;
  5. for (int i = 0; i < n - 1; i++) {
  6. // 不断计算能跳到的最远距离
  7. farthest = Math.max(farthest, i + nums[i]);
  8. // 可能碰到了 0,卡住跳不动了
  9. if (farthest <= i) {
  10. return false;
  11. }
  12. }
  13. return farthest >= n - 1;
  14. }
  15. }
  16. // 详细解析参见:
  17. // https://labuladong.github.io/article/?qno=55

45. 跳跃游戏 II

跳跃游戏 - 图2