[抄题]:

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. Ubuntu下安装为知笔记

    之前在Windows下用的是有道云笔记,但是后来开始习惯使用Linux开发,有道云官方并没有提供Ubuntu的版本,所以权衡之下,选择了为知笔记,安装步骤: sudo add-apt-reposito ...

  2. 剑指offer-第七章面试案例1(字符串转换为整型)

    //将字符串转换为整型 //思路:特殊的输入测试: //1,考虑字符串是否为空.2.字符串问空的时候的返回0,和真实的返回0直键的区别.3,字符串中出现0~9的字符处理 //4.字符串中出现*,¥等一 ...

  3. SqlServer 数据表数据移动

    描述:将Test1表中的数据放到Test2表中 1.Test2表不存在 select A,B,C insert into Test2 from Test1 select * into Test2 fr ...

  4. python 的os的总结

    转:http://www.cnblogs.com/BeginMan/p/3327291.html

  5. ORA-28595: Extproc 代理: DLL 路径无效解决办法

    报错信息: ORA-28595: Extproc 代理: DLL 路径无效 ORA-06512: 在 "SDE.ST_GEOMETRY_SHAPELIB_PKG", line 70 ...

  6. window下安装mysql

    参考地址: https://www.cnblogs.com/lmh2072005/p/5656392.html http://www.jb51.net/article/90302.htm 一.下载安装 ...

  7. css中伪类和伪元素的区别

    转载:http://www.cnblogs.com/ihardcoder/p/5294927.html CSS3伪类和伪元素的特性和区别   前端er们大都或多或少地接触过CSS伪类和伪元素,比如最常 ...

  8. android中状态栏透明

    设置 Acitivity 所在 window 的属性 @Override protected void onCreate(Bundle savedInstanceState) { super.onCr ...

  9. OPCDAAuto.dll的C#使用方法浅析

    上次研究了.Net版本的OPC API dll,这次我采用OPCDAAuto.dll来介绍使用方法.以下为我的源代码,有详细的注释无需我多言.编译平台:VS2008SP1.WINXP.KEPServe ...

  10. vc访问ACCESS数据库

    在现代软件开发中,数据库技术被越来越广泛应用,很多项目都存在着大量的数据需要存储,通常都会采用数据库来存储这些数据.最初,数据库厂商推出一个新的数据库产品时,相应的,他会为程序员提供一套访问该数据库的 ...