标签:程序 pac 空间 else length 结果 输出 图片 .com
[抄题]:
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: True Explanation: You could modify the first4
to1
to get a non-decreasing array.
Example 2:
Input: [4,2,1] Output: False Explanation: You can‘t get a non-decreasing array by modify at most one element.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道怎么改啊
[一句话思路]:
既然只允许修改一位,“前天”是否异常,决定了应该修改“昨天”还是“今天”
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
头回见:既然只允许修改一位,“前天”是否异常,决定了应该修改“昨天”还是“今天”
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution { public boolean checkPossibility(int[] nums) { //cc if (nums == null || nums.length == 0) { return false; } //ini int count = 0; //for loop for (int i = 1; i < nums.length && count <= 1; 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 return count <= 1; } }
665. Non-decreasing Array只允许修改一位数的非递减数组
标签:程序 pac 空间 else length 结果 输出 图片 .com
原文地址:https://www.cnblogs.com/immiao0319/p/8901802.html