LeetCode 665
Non-decreasing Array
- Problem Description:
给出一个数组,判断该数组能否成为非递减数组(在最多修改一个元素的前提下)。
具体的题目信息:
https://leetcode.com/problems/non-decreasing-array/description/ - Example:
(1)[2, 3, 3, 2, 4]——true
(2)[-1, 4, 2, 3]——true
(3)[3, 4, 2, 3]——false - Solution:
- 解题思路:根据样例才推出判断条件的TAT
- 编程实现:
class Solution {
public:
bool checkPossibility(vector<int>& nums) {
if (nums.size() == 0||nums.size() == 1) return true;
int count = 0;
for (int i = 0; i <= nums.size()-2; i++) {
if (nums[i]>nums[i+1]) {
if (count == 0) {
if (i >= 1 && i <= nums.size()-3) {
if (nums[i-1]>nums[i+1]&&nums[i]>nums[i+2])
return false;
}
} else {
return false;
}
count++;
}
}
return true;
}
};