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 first4
to1
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]:
[思维问题]:
不知道怎么改啊
[一句话思路]:
既然只允许修改一位,“前天”是否异常,决定了应该修改“昨天”还是“今天”
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 必须要有“昨天”异常的前提才有后续的操作。所以要把“昨天”之后的全都括起来
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼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 ...
随机推荐
- 【MFC】MFC绘图不闪烁——双缓冲技术
MFC绘图不闪烁——双缓冲技术[转] 2010-04-30 09:33:33| 分类: VC|举报|字号 订阅 [转自:http://blog.163.com/yuanlong_zheng@126/ ...
- tab显示不同数据
效果 核心代码 [js] [#escape x as (x)!?html]<!doctype html><html lang="zh-CN"><hea ...
- 2017-2018-2 20179215《网络攻防实践》seed缓冲区溢出实验
seed缓冲区溢出实验 有漏洞的程序: /* stack.c */ /* This program has a buffer overflow vulnerability. */ /* Our tas ...
- centos6.5 安装sftp
1.创建sftp组 : groupadd sftp 2.创建一个sftp用户,用户名为andy : useradd -g sftp -s /bin/false andy 3.修改密码: pas ...
- ArcGIS_Server的安装
1.双击ArcGIS_for_Server_Windows_103_142101.exe 2.下一步 3.关闭 4.Win10系统弹出询问框是否更改程序,点击“”是“” 5.开始安装程序,点击next ...
- 15.Selenium+Python滑动解锁小案例
1.代码实现 from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChai ...
- 平均分割list
private static List<List<Integer>> splitList(List<Integer> lists,int limit){ int s ...
- 关于ListView和GridView的应用
这两篇博文分别讲的很好: ListView: http://www.cnblogs.com/noTice520/archive/2011/12/05/2276379.html GridViw: htt ...
- dxjk中 支付宝二维码支付 git 存疑
线上的vendor/latrell/alipay 文件拉取不了至本地,失去了git监控 要想本地使用 1.注释掉config/app.php 'providers' 下的Latrell模块 2.下载线 ...
- c# HttpWebResponse 调用WebApi
public static class WebApiCaller { public static string HttpPost(string url, string body) { try { // ...