Given an array withn
integers, your task is to check if it could become non-decreasing by modifyingat most1
element.
We define an array is non-decreasing ifarray[i] <= array[i + 1]
holds for everyi
(1 <= i < n).
https://leetcode.com/problems/non-decreasing-array/description/
题意要求判断数组是否满足最多修改一个元素的前提下保证是非递减的。
class Solution {public boolean checkPossibility(int[] nums) {int count = 0;for (int i = 1; i < nums.length; i++) {if (nums[i - 1] > nums[i]) {count++;if (i - 2 < 0 || nums[i - 2] < nums[i]) nums[i - 1] = nums[i];else nums[i] = nums[i - 1];}}return count <= 1;}}