7ms C solution,but the arrary was changed


  • 0
    F

    bool canJump(int A[], int n) {

    int p=0;
    int i;
    while(p<n-1){
        if(A[p]!=0&&A[p]!=-1)
            p=p+A[p];
        else if(A[p]==0){
            A[p]=-1;
          for(i=1;i<=p;i++){
            if(A[p-i]!=i&&A[p-i]!=0){
                p=p-i;
                break;
            }
            else
                A[p-i]=-1;
          }
        }
        else
           return false;
        
    }
    if(p>=n-1)
        return true;
    else
        return false;
    

    }


Log in to reply
 

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