给定一个长度为 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. 获取url中的参数,函数封装,随拿随用

    获取 function getAllUrlParams(url) { var queryString = url ? url.split('?')[1] : window.location.searc ...

  2. fiddler报错:creation of the root certificate was not successful 证书安装不成功

    fiddler提示:creation of the root certificate was not successful 证书安装不成功 首先 找到Tools——>Options 在弹出的菜单 ...

  3. 为什么Java中的String是设计成不可变的?(Why String is immutable in java)

    There are many reasons due to the string class has been made immutable in Java. These reasons in vie ...

  4. PHP面向对象访问修饰符的基本了解

    l 文档的介绍: 对属性或方法的访问控制,是通过在前面添加关键字 public(公有),protected(受保护)或 private(私有)来实现的.被定义为公有的类成员可以在任何地方被访问.被定义 ...

  5. linux最基础最常用的命令快速手记 — 让手指跟上思考的速度(三)

    这一篇作为姐妹篇的第三篇,废话不多说,我觉得这个比mysql的还要重要,为什么,一旦你摊上linux 敲键盘输入命令简直是要飞的速度,不断的卡壳查命令,效率太低了,而且非常严重的影响思绪,思绪! 某些 ...

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

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

  7. Leetcode319. Bulb Switcher灯泡开关

    初始时有 n 个灯泡关闭. 第 1 轮,你打开所有的灯泡. 第 2 轮,每两个灯泡你关闭一次. 第 3 轮,每三个灯泡切换一次开关(如果关闭则开启,如果开启则关闭).第 i 轮,每 i 个灯泡切换一次 ...

  8. mac下企业邮件不能发送的问题

    1,选用服务器:smtp.example.qq.com     使用ssl      用密码      端口:465

  9. hdu1693 Eat the Trees [插头DP经典例题]

    想当初,我听见大佬们谈起插头DP时,觉得插头DP是个神仙的东西. 某大佬:"考场见到插头DP,直接弃疗." 现在,我终于懂了他们为什么这么说了. 因为-- 插头DP很毒瘤! 为什么 ...

  10. HZOI2019 砍树 整除分块

    题目链接:https://www.cnblogs.com/Juve/articles/11207540.html(密码你懂的)——————————>> 这题... 一开始想的二分,但此题不 ...