给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列。

我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n),满足 array[i] <= array[i + 1]。

示例 1:

输入: [4,2,3] 输出: True 解释: 你可以通过把第一个4变成1来使得它成为一个非递减数列。

示例 2:

输入: [4,2,1] 输出: False 解释: 你不能在只改变一个元素的情况下将其变为非递减数列。

说明:  n 的范围为 [1, 10,000]。

class Solution {
public:
bool checkPossibility(vector<int>& nums) {
int cnt = 0;
int len = nums.size();
for(int i = 1; i < len && cnt <= 1; i++)
{
if(nums[i - 1] > nums[i])
{
cnt++;
if(i - 2 < 0 || nums[i - 2] <= nums[i])
{
nums[i - 1] = nums[i];
}
else
{
nums[i] = nums[i - 1];
}
}
}
return cnt <= 1;
}
};

Leetcode665.Non-decreasing Array非递减数组的更多相关文章

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

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

  2. 665. Non-decreasing Array只允许修改一位数的非递减数组

    [抄题]: Given an array with n integers, your task is to check if it could become non-decreasing by mod ...

  3. 顺序表习题(1)-打印非递减数组a与b的升序并集(去除重复元素)

    void Print_Union(SqList a,SqList b) { , q = ; //初始化指针 ; //记录上一次打印的元素 while (p!=a.length&&q!= ...

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

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

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

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

  6. HDU 5532 Almost Sorted Array (最长非递减子序列)

    题目链接 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap s ...

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

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

  8. LeetCode 665. 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. System.Web.Mvc.ViewEngineResult.cs

    ylbtech-System.Web.Mvc.ViewEngineResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, P ...

  2. mui.fire()用法

    作用: mui.fire() 可以触发目标窗口的自定义事件 mui.fire( 目标窗口的webview , '自定义事件名' ,{参数列表}:) 目标窗口监听这个自定义事件 window.addEv ...

  3. <jsp:forward page=""></jsp:forward>标签失效异常

    解决方案:在web.xml <filter-mapping>          <filter-name>struts2</filter-name>         ...

  4. redhat4.4下安装GMT4.5.11

    GMT是地学界常用的开源软件,不仅是因为其开源的特性,还有着独特的魅力. 所需要的软件如下 安装步骤: 1. Put the soft packages in one folder, i.e. /ho ...

  5. java基础之Integer包装类

    Integer类概述: Integer 类在对象中包装了一个基本类型 int 的值 该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他 ...

  6. 基于baseline和stochastic gradient descent的个性化推荐系统

    文章主要介绍的是koren 08年发的论文[1],  2.1 部分内容(其余部分会陆续补充上来). koren论文中用到netflix 数据集, 过于大, 在普通的pc机上运行时间很长很长.考虑到写文 ...

  7. Fiilter

    过滤器 过滤请求和响应 作用:        自动登录.        统一编码.        过滤关键字        .... Filter是一个接口 编写filter步骤: 1.编写一个类 a ...

  8. php连接数据库插入数据

    <form action="updata.php" method="post"> 姓名:<input type="text" ...

  9. xshell添加脚本

    ##### xshell添加脚本```属性连接 - 用户身份验证 - 登陆脚本 - 添加等待:[usmshell]$发送:open 212 //212是指188那台机器的ID再添加一个等待:passw ...

  10. 提示框插件layer的使用讲解

    官方网站:http://layer.layui.com/ 在页面中引入: <script src="js/layer-v3.0.3/layer/layer.js">&l ...