Create 665.cpp

This commit is contained in:
Kirigaya Kazuto 2022-05-22 19:11:08 +08:00 committed by GitHub
parent 2a420466b6
commit 63e2720fe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

22
LeetCode-CN/665.cpp Normal file
View File

@ -0,0 +1,22 @@
class Solution {
public:
bool checkPossibility(vector<int>& nums) {
int nrev = 0;
for(int i=1; i<nums.size(); i++) {
if (nums[i-1] > nums[i]) {
if (nrev) return false;
nrev++;
if (i < 2) nums[i-1] = nums[i];
else if (nums[i-2] <= nums[i]) {
nums[i-1] = nums[i-2];
} else {
nums[i] = nums[i-1];
}
}
}
return nrev < 2;
}
};