share my java solution!


  • 0
    T
    class Solution {
        public boolean checkPossibility(int[] nums) {
            if(nums == null || nums.length<=2){
                return true;
            }   
            int index = 0;
            int count = 0;
            for(int i=0;i<nums.length-1;i++){
                if(nums[i]>nums[i+1]){
                    count++;
                    index = i;
                }
                if(count>1){
                    return false;
                }
            }
            
            if(index==0 || index+1 == nums.length-1){
                return true;
            }
            return nums[index+1]>=nums[index-1] || nums[index+2]>=nums[index];
        }
    }
    

Log in to reply
 

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