LeetCode 665. 非递减数列(Non-decreasing Array)
665. 非递减数列
665. Non-decreasing Array
题目描述
给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列。
我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n),满足 array[i] <= array[i + 1]。
LeetCode665. Non-decreasing Array
示例 1:
输出: True
解释: 你可以通过把第一个 4 变成 1 来使得它成为一个非递减数列。
示例 2:
输出: False
解释: 你不能在只改变一个元素的情况下将其变为非递减数列。
说明: n 的范围为 [1, 10,000]。
Java 实现
class Solution {
public boolean checkPossibility(int[] nums) {
if (nums == null || nums.length == 0) {
return true;
}
int count = 0;
for (int i = 1; i < nums.length && count <= 1; i++) {
if (nums[i - 1] > nums[i]) {
count++;
if (i - 2 >= 0 && nums[i - 2] > nums[i]) {
nums[i] = nums[i - 1];
} else {
nums[i - 1] = nums[i];
}
}
}
return count <= 1;
}
}
参考资料
- https://leetcode.com/problems/non-decreasing-array/
- https://leetcode-cn.com/problems/non-decreasing-array/
LeetCode 665. 非递减数列(Non-decreasing Array)的更多相关文章
- Java实现 LeetCode 665 非递减数列(暴力)
665. 非递减数列 给你一个长度为 n 的整数数组,请你判断在 最多 改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 < ...
- Leetcode 665.非递减数列
非递减数列 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i ...
- 【LeetCode】665. 非递减数列 Non-decreasing Array(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:数组,array,非递减,遍历,python,C++ 目录 题目描述 题目大意 解题方法 一.错误代码 二.举例分析 ...
- [Swift]LeetCode665. 非递减数列 | Non-decreasing Array
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- C#LeetCode刷题之#665-非递减数列( Non-decreasing Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3732 访问. 给定一个长度为 n 的整数数组,你的任务是判断在最 ...
- [LeetCode] Non-decreasing Array 非递减数列
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- Leetcode665.Non-decreasing Array非递减数组
给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n ...
- HDU 5532 Almost Sorted Array (最长非递减子序列)
题目链接 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap s ...
- [Leetcode][Python]33: Search in Rotated Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...
随机推荐
- 1054 The Dominant Color (20)(20 分)
Behind the scenes in the computer's memory, color is always talked about as a series of 24 bits of i ...
- Java 死锁以及死锁的产生
public class DeadLockSample { public static void main(String[] args) { DeadLock d1 = new DeadLock(tr ...
- 新建Class文件时,添加作者版权注释声明
以安装路径C盘为例,各版本路径如下: VS2015:C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTempla ...
- 反手来个K8S入门到跑路
layout: post title: 反手来个K8S入门到跑路 category: linux date: 2019-06-09 tags: linux k8s 反手来个K8S入门到跑路 前言 放假 ...
- go的flag模块使用例子
package main import ( "flag" "fmt" "strconv" ) func main() { port := f ...
- #C++初学记录(贪心算法#二分查找)
D - Aggressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 < ...
- 深度学习面试题29:GoogLeNet(Inception V3)
目录 使用非对称卷积分解大filters 重新设计pooling层 辅助构造器 使用标签平滑 参考资料 在<深度学习面试题20:GoogLeNet(Inception V1)>和<深 ...
- office project visio 2019
office2019,2016,2013,2010 Visio.Project 各版本下载激活!点我! http://blog.sina.com.cn/s/blog_170abd40a0102yah2 ...
- DELPHI给整个项目加编译开关
DELPHI给整个项目加编译开关 project--options
- C++操作Mysql数据库/Linux下
本文链接:https://blog.csdn.net/Tanswer_/article/details/72796570想用C++写项目,数据库是必须的,所以这两天学了一下C++操作Mysql数据库的 ...