刚开始的思路是,列举所有情况,因为在数组里直接删除一个元素的操作我不会,就想着让上一个值赋给这个值或者这个值赋给下一个值,但是发现情况太多,并且边界条件太过复杂,本来还在最后放一个哨兵,更复杂了,放弃
第二个思路是,用一个栈,把数组里的数据弹出去放进栈里,然后通过比较栈顶的元素和数组里的第i位大小判断,但是发现情况还是太多,没法列举完,放弃。
shopee的题真不错
答案
class Solution {
public boolean checkPossibility(int[] nums) {
int cnt = 0;
for (int i = 1; i < nums.length && cnt <= 1; i++) {
if (nums[i] >= nums[i - 1]) continue;
cnt++;
if (i - 1 > 0 && nums[i] < nums[i - 2]) {
nums[i] = nums[i - 1];
} else {
nums[i - 1] = nums[i];
}
}
return cnt <= 1;
}
}
作者:hu-li-hu-wai
链接:https://leetcode-cn.com/problems/remove-one-element-to-make-the-array-strictly-increasing/solution/tan-xin-by-hu-li-hu-wai-1zni/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
这个是我想找的规律 这个人太强了
还有一种方法 前面的人写的都是这种方法 没看懂 好像用了辅助数组
class Solution {
public:
int a[50001];
bool canBeIncreasing(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n; i ++) {
int top = 0;
for (int j = 0; j < n; j ++) {
if (j == i) continue;
++ top;
a[top] = nums[j];
}
bool ok = false;
for (int j = 1; j < top; j ++)
if (a[j] >= a[j + 1]) ok = true;
if (ok == false) return true;
}
return false;
}
};