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