[C] Simple C solution (Accepted)


  • 0
    P

    Traverse all n entry in the array.
    If A[i] > A[i+1], then check whether this condition can be resolved by one entry change.

    bool checkPossibility(int* nums, int numsSize) {
        int count = 0, i = 0;
        for(i=0;i<numsSize-1;i++)
        {
            if (nums[i]>nums[i+1])
            {
                count++;
                if (count>=2)
                    return false;
                if ( (i!=0 && i+2<numsSize && nums[i+2]<nums[i]) && 
                     (i-1>=0 && nums[i+1]<nums[i-1]) )
                    return false;
            }
        }
        return true;
    }
    

Log in to reply
 

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