leetcode:Non-decreasing Array

Non-decreasing Array

改变序列中至多一个数,使之变成不增序列array[i] <= array[i + 1]

def nondecreasing(nums):
    one, two = nums[:], nums[:]
    for i in range(len(nums) - 1):
        if nums[i] > nums[i + 1]:
            one[i] = nums[i + 1]
            two[i + 1] =  nums[i]
            break
    return one == sorted(one) or two == sorted(two)

猜你喜欢

转载自blog.csdn.net/weixin_38758969/article/details/80336839