Jump Game II

来源:互联网 发布:迅雷 mac 速度0 编辑:程序博客网 时间:2024/06/04 18:25
class Solution {
public:
    int jump(vector<int>& nums) {
        if(nums.size()==1) return 0;
        
        int maxreach=nums[0];
        int step=1;
        int start=0,newmax=0;
       while(maxreach<nums.size()-1) 
       {
           //newmax = maxreach;
           for(int j=start+1;j<=maxreach;j++)   //j是,不是从1开始。
          {
           newmax=max(newmax,nums[j]+j);
          }
          start=maxreach;   
          step++;
         //maxreach=max(maxreach,newmax);
         maxreach=newmax;
       }
       return step;
    }
};
0 0