luoheng
10/14/2019 - 10:35 AM

checkPossibility

func checkPossibility(nums []int) bool {
    sum := 0
    for i := 0; i < len(nums)-1; i++ {
        if nums[i] > nums[i+1] {
            if sum == 1 {
                return false
            }
            if i == 0 || i == len(nums)-2 || i >= 1 && nums[i+1] >= nums[i-1] || i < len(nums)-2 && nums[i] <= nums[i+2] {
                sum++
            } else {
                return false
            }
        }
    }
    return true
}