0665.cpp (600B)
1 class Solution { 2 public: 3 bool checkPossibility(const vector<int> &nums) const { 4 const int n = size(nums); 5 int cnt1 = 0, cnt2 = 0; 6 7 for (int i = 1, prev = nums[0]; i < n && cnt1 <= 1; i++) { 8 if (nums[i] < prev) 9 cnt1++; 10 else 11 prev = nums[i]; 12 } 13 14 if (cnt1 <= 1) return true; 15 16 for (int i = n - 2, prev = nums[n - 1]; i >= 0 && cnt2 <= 1; i--) { 17 if (nums[i] > prev) 18 cnt2++; 19 else 20 prev = nums[i]; 21 } 22 23 return cnt2 <= 1; 24 } 25 };