题目描述:

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 first 4 to 1 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.

Note: The n belongs to [1, 10,000].

要完成的函数:

bool checkPossibility(vector<int>& nums)

说明:

1、这道题目给定一个vector,要判断这个vector至多改变一个元素的值之后,是不是变成了非减序列。

首先笔者想的是,比如序列[1,4,2,3],这个序列改变4的值也就可以了,这样子的序列中间必然会有一个凸起,4就是这个凸起。

那我们可以先找到这个凸起,从前往后找跟从后往前找,如果只有一个凸起的话,那么就接着判断,如果有多个凸起,那么必定不能只改一个元素。

部分代码如下:

        int s1=nums.size();
int i=,j=s1-;
while(i<s1-)
{
if(nums[i]<=nums[i+])
i++;
else
break;
}
if(i==j)//当整个序列非降序排列
return true;
while(j>i)
{
if(nums[j]>=nums[j-])
j--;
else
break;
}
if((j-)!=i)//如果中间有多个元素
return false;

这样子就记录了i和j的值。

2、当确实只有一个凸起时,我们进行下一步判断。

还是以上面提到的序列为例子,[1,4,2,3],i=1,j=2,这个凸起的形成是由于nums[i]>nums[j],那我们可以改i这一位的值,也可以改j这一位的值,使得nums[i]<=nums[j]。

怎么判断什么时候要改i的值,什么时候要改j的值?

比如[3,4,2,5],i=1,j=2,这时候由于nums[i]的前一位3,大于nums[j]=2,所以我们只能修改j这一位的值,而不能修改i这一位的值。

那如果nums[i]的值,还是比nums[j]的下一位大呢,这时候我们就算修改j这一位的值,修改完也不能形成非减序列,这时候就要返回false。

如果nums[i]的值,小于等于nums[j]的下一位,那这时候就要返回true了。

还有另一种情况,如果nums[i]的前一位,小于等于nums[j],比如上面提到的[1,4,2,3],i=1,j=2,那这时候我们就可以修改i的值了,直接返回true即可。

代码如下:

        if(i!=)
{
if(nums[i-]>nums[j])//只能改j这一位
{
if(j==s1-)
return true;
if(nums[i]>nums[j+])
return false;
return true;
}
else
return true;
}
return true;//如果i==0,那么直接修改nums[i]的值就可以了

上述代码虽然考虑的过程繁琐了点,但是实测38ms,beats 86.96% of cpp submissions,效果还是可以的。

3、附上完整代码,分享给大家,如下:

    bool checkPossibility(vector<int>& nums)
{
int s1=nums.size();
int i=,j=s1-;
while(i<s1-)
{
if(nums[i]<=nums[i+])
i++;
else
break;
}
if(i==j)
return true;
while(j>i)
{
if(nums[j]>=nums[j-])
j--;
else
break;
}
if((j-)!=i)
return false;
if(i!=)
{
if(nums[i-]>nums[j])//只能改j这一位
{
if(j==s1-)
return true;
if(nums[i]>nums[j+])
return false;
return true;
}
elsereturn true;
}
return true;
}

leetcode-665-Non-decreasing Array的更多相关文章

  1. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

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

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

  3. LeetCode 665. Non-decreasing Array (不递减数组)

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

  4. Leetcode 665. Non-decreasing Array(Easy)

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

  5. LeetCode算法题-Non-decreasing Array(Java实现)

    这是悦乐书的第283次更新,第300篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第151题(顺位题号是665).给定一个包含n个整数的数组,您的任务是通过修改最多1个元 ...

  6. 【LeetCode】896. Monotonic Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode题解】数组Array

    1. 数组 直观地看,数组(Array)为一个二元组<index, value>的集合--对于每一个index,都有一个value与之对应.C语言中,以"连续的存储单元" ...

  8. LeetCode 238. Product of Array Except Self (去除自己的数组之积)

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  9. LeetCode 108. Convert Sorted Array to Binary Search Tree (有序数组转化为二叉搜索树)

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目 ...

  10. LeetCode 88. Merge Sorted Array(合并有序数组)

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

随机推荐

  1. jdk8中LocalDateTime,LocalDate,LocalTime等日期时间类

    package com.zy.time; import org.junit.Test; import java.time.*; import java.time.format.DateTimeForm ...

  2. java的static块及相关内容

    原文地址:http://blog.csdn.NET/lubiaopan/article/details/4802430     感谢原作者! static{}(即static块),会在类被加载的时候执 ...

  3. [Training Video - 6] [File Reading] [Java] Read Excel File Using Apache POI API

    读取以下两种格式的Excel : *.xls  and *.xlsx 用Apache POI API来实现,需要用到 HSSF 和 XSSF 的类库 HSSF is the POI Project's ...

  4. [GO]panic的应用

    对于异常的处理,error表示的是不太致使的错误,但是如果遇到数组越界或者是空指针这种会导致程序崩溃无法恢复的错误时,就需要使用以panic了 我们不应该使用panic去报error的错误,而是只使用 ...

  5. [GO]面向对象编程

    对于面向对象编程的支GO语言的设计简洁而优雅,因为,GO语言没有沿袭传统面向对象中的诸多概念 比如继承(不支持继承,尽管匿名字段的内存布局和行为类似继承,但它并不是继承) 尽管GO语言没有封装.继承. ...

  6. Linux比较操作符

    http://blog.csdn.net/ithomer/article/details/6836382

  7. Java中BufferedReader和scanner的对比

    Scanner 和BufferedReader同样能实现将键盘输入的数据送入程序, import java.io.*;import java.util.Scanner;public class C { ...

  8. PHP 5.2 5.3 5.4 5.5 memcache dll扩展

    在windows下PHP5.2版本的memcache扩展dll文件好找,5.3的可能不是很好找,这里提供PHP5.2.5.3.5.4.5.5的php_memcache.dll扩展,需要的可以下载. 全 ...

  9. Matlab图像处理教程

    虽然典型算法的开发是基于理论支持的,但这些算法的实现几乎总是要求参数估计,并常常进行算法修正与候选求解方案的比较. MATLAB由LINPACK和EISPACK项目开发,最初用于矩阵处理.今天,MAT ...

  10. 编写高质量代码改善C#程序的157个建议——建议135: 考虑使用肯定性的短语命名布尔属性

    建议135: 考虑使用肯定性的短语命名布尔属性 布尔值无非就是True和False,所以应该用肯定性的短语来表示它,例如,以Is.Can.Has作为前缀. 布尔属性正确命名的一个示例如下: class ...