題目來源:https://leetcode.com/problems/jump-game/算法
個人程序(過度複雜了)ide
class Solution { public: bool canJump(vector<int>& nums) { /*ALL IS ABOUT SKIPPING THE ZERO-Prison.mark all the figures before zero to see whether it can skip the zero.WE know that if the start is marked as unable to skip,it fails.however,we should consider the ability of reaching,too.*/ int length=nums.size(); if(length==1) return true; int ability[length]; for(int i=0;i<length;i++){ if(nums[i]!=0) ability[i]=1; else ability[i]=0; } for(int i=length-2;i>=0;i--){ if(ability[i]==0){ //cout<<i<<" "<<nums[i]<<endl; continue; } else{ int j=1; ability[i]=0; //cout<<i<<" "<<nums[i]<<endl; if(i+nums[i]>=length-1) { //cout<<"!"<<endl; ability[i]=1; continue; } while(i+j<length&&j<=nums[i]){ if(ability[i+j]!=0) { //cout<<"!"<<endl; ability[i]=1; break; } j++; } } //cout<<i<<' '<<ability[i]<<endl; } for(int i=0;i<length;i++) cout<<ability[i]<<endl; if(ability[0]==1) return true; else return false; } };
討論裏看到一個很好的代碼:https://leetcode.com/problems/jump-game/discuss/318606/6line-C%2B%2B-beat-99-O(N)time-O(1)-space-easy-to-understandspa
class Solution { public: bool canJump(vector<int>& nums) { int size = nums.size(); int k = nums[0]; for(int i = 0; i <= k; ++i) { if((i+nums[i]) >= (size-1)) return true; else k = max(k, i+nums[i]); } return false; } };
這裏的k能夠理解爲起點所可及的範圍。在k範圍內的點都是可達到的(循環出口改成 k>=(size-1)可能更好理解)code
算法的思路以下:blog
1 class Solution { 2 public: 3 bool canJump(vector<int>& nums) { 4 int size = nums.size();//肯定點的個數 5 int k = nums[0];//這裏的k是原點的原始範圍 6 for(int i = 0; i <= k; ++i) {//對可及範圍內的點一一遍歷,實際上這個過程剛好也是隨着範圍的擴大而不斷向右的 7 if((i+nums[i]) >= (size-1)) return true;//當範圍達到最後一個點時即成功 8 else k = max(k, i+nums[i]);//i+nums[i]是當前點所能達到的最大範圍,此時其也被併入原點的可及範圍中 9 } 10 return false;//若是上面的擴充過程沒有達到終點,則判斷不可及 11 } 12 };