leetcode665. 非递减数列

class Solution {
    public boolean checkPossibility(int[] nums) {
        int n = nums.length;
        int p=0,q=0;
        for(int i = 0;i<n-1;i++){
            if(nums[i]>nums[i+1]){
                p = i;
                q = i+1;
                break;
            }
        }
        if(q==0 && q==0)return true;
        int flag = 0;
        int t=-0x3f3f3f3f;
        for(int i = 0;i<n;i++){
            if(i==p)continue;
            if(nums[i]<t){
                flag++;
                break;
            }
            t=nums[i];
        }
        t=-0x3f3f3f3f;
        for(int i = 0;i<n;i++){
            if(i==q)continue;
            if(nums[i]<t){
                flag++;
                break;
            }
            t=nums[i];
        }
        return flag<2;
    }
}

你可能感兴趣的:(Leetcode,leetcode)