Java Solution Beats 100%.


  • 0
    H
    class Solution {
        public boolean checkPossibility(int[] nums) {
            int num = 0;
            int index = 0;
            for(int i = 0; i < nums.length-1; i++){
                if(nums[i] > nums[i+1]){
                    num ++;
                    if(num > 1) return false;
                    index = i;
                }
            }
            
            if(index == 0) return true;
            
            else{
                if(index +2 >= nums.length || nums[index+2] > nums[index]) return true;
                if(nums[index+1] < nums[index-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.