[抄题]:

Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.

We define an array is non-decreasing if array[i] <= array[i + 1] holds for every i (1 <= i < n).

Example 1:

Input: [4,2,3]
Output: True
Explanation: You could modify the first 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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不知道怎么改啊

[一句话思路]:

既然只允许修改一位,“前天”是否异常,决定了应该修改“昨天”还是“今天”

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 必须要有“昨天”异常的前提才有后续的操作。所以要把“昨天”之后的全都括起来

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

头回见:既然只允许修改一位,“前天”是否异常,决定了应该修改“昨天”还是“今天”

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public boolean checkPossibility(int[] nums) {
//cc
if (nums == null || nums.length == 0) {
return false;
} //ini
int count = 0; //for loop
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 - 1] = nums[i];
}else {
nums[i] = nums[i - 1];
}}
} //return count <= 1
return count <= 1;
}
}

665. Non-decreasing Array只允许修改一位数的非递减数组的更多相关文章

  1. Leetcode665.Non-decreasing Array非递减数组

    给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n ...

  2. [LeetCode] Non-decreasing Array 非递减数列

    Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...

  3. 【LeetCode】665. 非递减数列 Non-decreasing Array(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:数组,array,非递减,遍历,python,C++ 目录 题目描述 题目大意 解题方法 一.错误代码 二.举例分析 ...

  4. LeetCode 665. 非递减数列(Non-decreasing Array)

    665. 非递减数列 665. Non-decreasing Array 题目描述 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是 ...

  5. 在当前Server上找某某object,注意只需修改"要找的object"就可以使用

    ---在当前Server上找某某object,注意只需修改"要找的object"就可以使用EXEC sp_MSforeachdb 'use ? ;IF EXISTS(SELECT ...

  6. LeetCode 665. Non-decreasing Array (不递减数组)

    Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...

  7. centos lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress 安装phpmyadmin 定时备份mysql两种方法 第二十五节课

    centos  lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress  安装phpmyadmin  定时备份mysql两种方法  第二十五节 ...

  8. [Swift]LeetCode665. 非递减数列 | Non-decreasing Array

    Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...

  9. Leetcode 665.非递减数列

    非递减数列 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i ...

随机推荐

  1. uboot Makefile 文件源码分析

    Makefile 是一个神奇的文件 详情参考uboot配置和编译过程详解

  2. linux 权限之所有者所属组

    linux 如何改变文件属性与权限 我们知道档案权限对于一个系统的安全重要性,也知道档案的权限对于使用者与群组的相关性, 那如何修改一个档案的属性与权限呢? 我们这里介绍几个常用于群组.拥有者.各种身 ...

  3. Linux 下安装composer

    1.下载composer.phar文件. 2.将composer.phar文件上传linux. 3.执行 php composer.phar 4.全局安装:mv composer.phar /usr/ ...

  4. MySql必知必会实战练习(一)表创建和数据添加

    1.实战环境 windows 10 64位 mysql-8.0.13 mysql编辑和查看工具:NaviCat for MySql 表脚本文件: ########################### ...

  5. 转载 关于include尖括号和双引号的区别。

    对于使用尖括号( < >),预处理程序cpp在系统预设包含文件目录(如/usr/include)中搜寻相应的文件,而对于使用双引号(“ ”),cpp在当前目录中搜寻头文件,这个选项的作用是 ...

  6. jslinq 使用总结

    最近一直在用 jslinq 感觉还是不错的.用于增强 Array.find() 上重点: 1: 引用 cnpm install jslinq --save (本人用淘宝镜像--npmFQ感觉也不快-- ...

  7. Android蓝牙UUID简要

    UUID是"Universally Unique Identifier"的简称,通用唯一识别码的意思.对于蓝牙设备,每个服务都有通用.独立.唯一的UUID与之对应.也就是说,在同一 ...

  8. eclipse “”base revision” vs. “latest from repository”

    base revision(基本版本):代表的是最近一次从svn服务器上面获取的版本内容:本质还是本地版本,只不过这个版本是上次从服务器上面获取的. lastest from resource(资源库 ...

  9. Linux安装微信

    地址:http://www.toutiao.com/i6362126617556288001/#6649976-tsina-1-90079-4471e2b057b5019ad452c722f04bba ...

  10. 减少CXF日志打印

    场景:项目中引用cxf发布服务,服务调用产生的日志实在是太多了,实在是不能忍 官方文档:http://cxf.apache.org/docs/debugging-and-logging.html#De ...