665. The non-decreasing number of columns
You give a length of an array of integers of n, is determined in a case where you change up an element of the array can become a non-decreasing number of columns.
We thus defined is a non-decreasing number of columns: array for all i (1 <= i <n), to meet the total array [i] <= array [i + 1].
Example 1:
Input: nums = [4,2,3]
Output: true
explanation: first you could be by a 41 to make it a non-decreasing number of columns.
Example 2:
Input: nums = [4,2,1]
Output: false
Explanation: You can not only change in a case where the element which becomes non-decreasing number of columns.
Description:
1 <= n <= 10 ^ 4
- 10 ^ 5 <= nums [i ] <= 10 ^ 5
submitted by 73,484 times 16,232 times
class Solution {
public boolean checkPossibility(int[] nums) {
if(nums.length<=2) return true;
int i;
boolean flag = true;
for(i = 0; i< nums.length-1; i++){
if(nums[i]>nums[i+1]){
if(flag){
if(i>0 && nums[i-1]>nums[i+1]){
//前面的值大就让我下一个等于我当前的
nums[i+1] = nums[i];
} else{
//前面的小就让后面这俩相等
nums[i] = nums[i+1];
}
flag = false;
} else{
return false;
}
}
}
return true;
}
}