1 class Solution 2 { 3 public: 4 bool canJump(vector<int>& nums) 5 { 6 int sz=nums.size(); 7 if(sz<2) 8 return true; 9 int judge=sz-1; 10 int curreach=0,i=0; 11 for(;i<sz&&i<=curreach;i++) 12 { 13 curreach=max(curreach,nums[i]+i); 14 if(curreach>=judge) 15 return true; 16 } 17 return false; 18 } 19 };
跳跳问题,很骚,这个方法值得学习,用最大跳跃距离是否超过数组最终位置来决定,一步一步跳,每次更新最大跳跃位置,在当前位置和最大跳跃位置之间顺序跳。