问题:
Given an array with n
integers, your task is to check if it could become non-decreasing by modifying at most 1
element.
We define an array is non-decreasing if array[i] <= array[i + 1]
holds for every i
(1 <= i < n).
Example 1:
Input: [4,2,3]Output: TrueExplanation: You could modify the first 4 to 1 to get a non-decreasing array.
Example 2:
Input: [4,2,1]Output: FalseExplanation: You can't get a non-decreasing array by modify at most one element.
Note: The n
belongs to [1, 10,000].
解决:
① 题目要求判断数组是否是非递减的,最多有1次修改数值的机会。统计出现array[i] > array[i + 1] 该条件的次数,如果大于2,则返回false,如果等于0,则返回true,其他情况分别作讨论即可。例如:
4,2,3
-1,4,2,3
2,3,3,2,4
当后面的数字小于前面的数字产生冲突后,有时候需要修改前面较大的数字(比如前两个例子需要修改4),有时候却要修改后面较小的那个数字(比如前第三个例子需要修改2);
判断修改哪个数字其实跟再前面一个数的大小有关系:
首先如果再前面的数不存在,比如例子1,4前面没有数字了,我们直接修改前面的数字为当前的数字2即可。
而当再前面的数字存在,并且小于当前数时,比如例子2,-1小于2,我们还是需要修改前面的数字4为当前数字2;
如果再前面的数大于当前数,比如例子3,3大于2,我们需要修改当前数2为前面的数3。这是修改的情况,由于我们只有一次修改的机会,所以使用一个标识,标识之前是否已经修改过了。
class Solution { //27ms
public boolean checkPossibility(int[] nums) { boolean ismodified = false; for (int i = 1;i < nums.length;i ++){ if (nums[i] < nums[i - 1]){ if (ismodified) return false; if (i - 2 < 0 || nums[i - 2] <= nums[i]){ nums[i - 1] = nums[i]; }else{ nums[i] = nums[i - 1]; } ismodified = true; } } return true; } }