Share my simple C++


  • 0
    L
    class Solution {
    public:
        bool canJump(vector<int>& nums) 
        {
             vector<bool> reach(nums.size(),0);
             if(nums.size()==0||nums[0]==0&&nums.size()!=1)return 0;
             reach[0]=1;
             int last=1,j=0;
             for(int i=0;i<nums.size();i++)
             {   
                 for( j=last;j<=nums[i]+i;j++)
                 {
                      if(i==last)return 0;  //at this case nums[i] is not reachable
                      reach[j]=1;
                     if(reach[nums.size()-1])return 1;
                 }
                 last=j;
             }
             return reach[nums.size()-1];
        }
        
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.