class Solution: def checkPossibility(self, nums: List[int]) -> bool: # modified 表示是否已经修改过 modified: bool = False for i in range(1, len(nums)): if nums[i] < nums[i - 1]: # 如果当前数小于 nums[i - 1] ,则需要修改一个数 if modified: # 如果已经修改过,则无法再修改,直...
一. 题目(medium) 改变数组里的一个值,实现非降序数组,也就是每个数字都比之前的数字大或者相等。 二. 思路 判断前两个数字 是否都大于当前的数字,来决定改谁的值。 当nums[i-1] > nums[i]时,就再看看是否 nums[i-2] > nums[i]; 二者都大于nums[i],那就改nums[i],才能符合题意; 否则改nums[i...
We define an array is non-decreasing ifarray[i] <= array[i + 1]holds for everyi(1 <= i < n). Example 1: 4 1 Example 2: Input: [4,2,1] Output: False Explanation: You can't get a non-decreasing array by modify at most one element. Note: Thenbelongs to [1, 10,000]. 题...
01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第151题(顺位题号是665)。给定一个包含n个整数的数组,您的任务是通过修改最多1个元素来检查它是否可以变为非递减。如果array [i] <= array [i + 1],且0 <= i <n,则我们定义一个数组是非递减的。例如: 输入:[4,2,3] 输出:true 说明:可以修...
题目地址:https://leetcode-cn.com/problems/non-decreasing-array/ 题目描述 给你一个长度为 n 的整数数组,请你判断在 最多 改变 1 个元素的情况下,该数组能否变成一个非递减数列。 我们是这样定义一个非递减数列的: 对于数组中所有的i (0 <= i <= n-2),总满足nums[i] <= nums[i + 1]。
Explanation: You could modify the first 4 to 1 to get a non-decreasing array. 1. 2. 3. Example 2: Input: [4,2,1] Output: False Explanation: You can't get a non-decreasing array by modify at most one element. 1. 2. 3. ...
Can you solve this real interview question? Non-decreasing Array - Given an array nums with n integers, your task is to check if it could become non-decreasing by modifying at most one element. We define an array is non-decreasing if nums[i] <= nums[i +
首先设置一个count变量,统计array[i] > array[i+1],如果count大于1就返回false; 对于不符合条件的元素,我们要给他重新赋值使整个数组可以满足单调递增 解法 Example1:Input:[4,2,3]Output:TrueExplanation:You could modify the first4to1togeta non-decreasing array. ...
题目 Given an array with n integers,your task is to checkifit could become non-decreasing by modifying at most1element.We define an array is non-decreasingifarray[i]<=array[i+1]holdsforevery i(1<=i<n). 给定一个整数数组,判定是否最多修改一个位置上的数字就可以让这个数组变成递增数组(包含...
4 to 1 to get a non-decreasing array. Example 2: Input:[4,2,1] Output:False Explanation:You can’t get a non-decreasing array by modify at most one element. Note:Thenbelongs to [1, 10,000]. 思路 从index=1开始遍历数组,判断nums[i]是否小于nums[i-1],如果小,就进行下面的更换: ...