665. Non-decreasing Array只允许修改一位数的非递减数组
[抄题]:
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 first4to1to 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]:
[思维问题]:
不知道怎么改啊
[一句话思路]:
既然只允许修改一位,“前天”是否异常,决定了应该修改“昨天”还是“今天”
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 必须要有“昨天”异常的前提才有后续的操作。所以要把“昨天”之后的全都括起来
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼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只允许修改一位数的非递减数组的更多相关文章
- Leetcode665.Non-decreasing Array非递减数组
给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n ...
- [LeetCode] Non-decreasing Array 非递减数列
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- 【LeetCode】665. 非递减数列 Non-decreasing Array(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:数组,array,非递减,遍历,python,C++ 目录 题目描述 题目大意 解题方法 一.错误代码 二.举例分析 ...
- LeetCode 665. 非递减数列(Non-decreasing Array)
665. 非递减数列 665. Non-decreasing Array 题目描述 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是 ...
- 在当前Server上找某某object,注意只需修改"要找的object"就可以使用
---在当前Server上找某某object,注意只需修改"要找的object"就可以使用EXEC sp_MSforeachdb 'use ? ;IF EXISTS(SELECT ...
- LeetCode 665. Non-decreasing Array (不递减数组)
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- centos lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress 安装phpmyadmin 定时备份mysql两种方法 第二十五节课
centos lamp/lnmp阶段复习 以后搬迁discuz论坛不需要重新安装,只需修改配置文件即可 安装wordpress 安装phpmyadmin 定时备份mysql两种方法 第二十五节 ...
- [Swift]LeetCode665. 非递减数列 | Non-decreasing Array
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- Leetcode 665.非递减数列
非递减数列 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i ...
随机推荐
- lwip 使用记录(1)
原子F429的lwip实验:网络实验8 NETCONN_TCP客户端实验 代码 //tcp客户端任务函数 static void tcp_client_thread(void *arg) { OS_C ...
- 【MFC】MFC绘图不闪烁——双缓冲技术
MFC绘图不闪烁——双缓冲技术[转] 2010-04-30 09:33:33| 分类: VC|举报|字号 订阅 [转自:http://blog.163.com/yuanlong_zheng@126/ ...
- 设计模式之原型(prototype)模式
相信大多数的人都看过<西游记>,对孙悟空拔毛变出小猴子的故事情节应该都很熟悉.孙悟空可以用猴毛根据自己的形象复制出很多跟自己一模一样的小猴兵出来,其实在设计模式中也有一个类似的模式,我们可 ...
- LeetCode Optimal Division
原题链接在这里:https://leetcode.com/problems/optimal-division/description/ 题目: Given a list of positive int ...
- 转载.怎样在Quartus II中转化HDL文件为bsf文件?
步骤1 新建或打开Quartus II工程,用QII自带文本编辑器打开HDL文件. 图1 用QII自带的文本编辑器打开HDL文件 步骤2 选择File>Create / Update>Cr ...
- ACM学习历程—HDU5701 中位数计数(中位数 && 计数排序)
http://acm.hdu.edu.cn/showproblem.php?pid=5701 这是这次百度之星初赛2B的第六题.之前白山云做过类似的题,省赛完回来,我看了一下大概就有这样的思路:首先枚 ...
- etcd服务端和客户端安装
下载地址: 服务端:http://download.csdn.net/download/wuxun1997/9841277 客户端:http://download.csdn.net/download/ ...
- LVS+Keepalived搭建
LVS+Keepalived搭建 原理说明 (推荐): http://www.cnblogs.com/likehua/archive/2014/06/19/3796849.html http://ou ...
- ZPAY个人收款助手使用说明
ZPAY个人收款助手使用说明 功能特点: ZPAY个人收款助手可实现收款成功后发送通知到服务器,网页可从服务器获取到付款状态从而完成操作. 可支持微信,支付宝的个人收款需求,无需支付宝微信认证,无需上 ...
- xunsearch安装使用
目录 1.下载 2.进入,安装 3.安装成功后,启动后台服务 4.安装PHP-SDK 安装步骤 1.下载 wget http://www.xunsearch.com/download/xunsearc ...