Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
non-decreasing-array.cpp 676 Bytes
Copy Edit Raw Blame History
kamyu authored 2017-08-28 22:48 +08:00 . Update non-decreasing-array.cpp
// Time: O(n)
// Space: O(1)
class Solution {
public:
bool checkPossibility(vector<int>& nums) {
int cnt = 0;
for (int i = 1, prev = nums[0]; i < nums.size(); ++i) {
if (prev > nums[i]) {
if (cnt++) {
return false;
}
if (i - 2 < 0 || nums[i - 2] <= nums[i]) {
prev = nums[i]; // nums[i - 1] = nums[i], prev = nums[i]
// } else {
// prev = nums[i - 1]; // nums[i] = nums[i - 1], prev = nums[i]
}
} else {
prev = nums[i];
}
}
return true;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search