Leetcode55:跳跃游戏(贪心)


来源:

 1 class Solution {
 2     public boolean canJump(int[] nums) {
 3         boolean flag = false;
 4         int n = nums.length;
 5         int pos = 0;//记录最远可到达的位置
 6         int i = 0;//遍历时的下标
 7         while(i<=pos && i//遍历时注意下标i不能超过当前能到的最远位置
 8             if(i + nums[i] > pos){
 9                 pos = i + nums[i];
10             }
11             i++;
12             if(pos>=n-1){
13                 flag = true;
14                 break;
15             }
16         }
17         return flag;
18     }
19 }