C solution,12ms,beat 100%


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

    0_1503999325730_捕获.PNG


Log in to reply
 

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