给定一个长度为 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. 怎样理解js数组中indexOf()的用法与lastIndexOf

    第一首先你运行一下它的js代码: var arr1=["大学","中庸","论语","孟子","诗" ...

  2. JS 根据今天的日期获取本周星期一与星期天的日期

    var now = new Date(); var nowTime = now.getTime() ; var day = now.getDay(); var oneDayTime = 24*60*6 ...

  3. 5 第k大元素

    原题网址:http://www.lintcode.com/zh-cn/problem/kth-largest-element/ 在数组中找到第k大的元素 注意事项 你可以交换数组中的元素的位置 您在真 ...

  4. vue alert插件(标题为图片)(自写)

    <template> <div class="modelSelf"> <div class="model" @click=&quo ...

  5. MyBatis注解开发-@Insert和@InsertProvider(转)

    @Insert和@InsertProvider都是用来在实体类的Mapper类里注解保存方法的SQL语句.不同的是,@Insert是直接配置SQL语句,而@InsertProvider则是通过SQL工 ...

  6. Vuejs之Component slot 插槽详解

    Vuejs的component的数据进行了沙箱隔离,除js全局变量如Math, Date之类外无法访问用户自定义的变量,所以使用component写组件或嵌套组件时明白变量的访问非常重要 编译作用域 ...

  7. 搭建一个Semantic-ui项目

    一.进入到项目目录 npm init 二.安装semantic-ui npm install semantic-ui --save 三.编译输出semantic-ui cd  ./semantic g ...

  8. 基于httpd2.2配置https

    本次演示使用一台主机实现,即自签自演 主机IP:192.168.1.105 开始配置: 1.创建私有CA # cd /etc/pki/CA # touch serial # touch index.t ...

  9. 记一次server 2008 R2的按装过程

    项目上一直在用的dell务器在去年年底突然出现系统过期,导致c盘的东西全部丢失.我们用激活工具激活,还是没能找回丢失的东西. 为了装这个系统,跟同事一起折腾了好些次,最后发现安装服务器的时候有个磁盘阵 ...

  10. PAT甲级——A1029 Median

    Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...