1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// @Title: 非递减数列 (Non-decreasing Array)
// @Author: 15816537946@163.com
// @Date: 2021-02-07 20:45:12
// @Runtime: 32 ms
// @Memory: 6.4 MB
func checkPossibility(nums []int) bool {
	cnt := 0
	for i := 0; i < len(nums)-1; i++ {
		x, y := nums[i], nums[i+1]
		if x > y {
			cnt++
			if cnt > 1 {
				return false
			}
			if i > 0 && y < nums[i-1] {
				nums[i+1] = x
			}
		}
	}

	return true
}