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].

/*
一开始想的有点简单,直接只是判断有多少个不符合的位置,并没有修复并检查修复后是否满足条件 所以 出现 3, 4, 2, 3的case就不行了
其实主要分两种情况:
2, 4, 2, 3
3, 4, 2, 3 判断当前的值是否比前一个值小,如果小的话, 再判断是否比 前前一个值 要小, 如果小,改变当前的值 使得其跟前一个值一样大,如果大,改变前一个值,使得它跟当前的值一样大。 */ class Solution {
public:
bool checkPossibility(vector<int>& nums) {
int len = nums.size();
int count = ;
// if (len == 0) return false;
for (int i = ; i < len; i++){
if (nums[i] < nums[i - ]){
count ++;
if (count > ){
return false;
}
if (nums[i] < nums[i - ] && (i - ) >= ){
nums[i] = nums[i - ];
}
else{
nums[i - ] = nums[i];
}
} }
return true;
}
};

Leetcode 665. Non-decreasing Array(Easy)的更多相关文章

  1. Leetcode 88. Merge Sorted Array(easy)

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

  2. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  3. LeetCode--Sort Array By Parity && N-Repeated Element in Size 2N Array (Easy)

    905. Sort Array By Parity (Easy)# Given an array A of non-negative integers, return an array consist ...

  4. [LeetCode] 038. Count and Say (Easy) (C++/Python)

    索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...

  5. [array] leetCode-26. Remove Duplicates from Sorted Array - Easy

    26. Remove Duplicates from Sorted Array - Easy descrition Given a sorted array, remove the duplicate ...

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

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

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

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

  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 448. Find All Numbers Disappeared in an Array -easy (重要)

    题目链接: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 题目描述: Give ...

随机推荐

  1. VScode快捷键、Chrome快捷键知识小总结和状态码

           一.VS code的一些快捷键 Shift + Delete    删除一整行 ctrl + Delete     删除光标之后到标点的数据 Delete          删除光标之后 ...

  2. 一张图教你读懂AI简史

  3. spring4笔记----“零配置”:spring提供的几个Annotation标注

    @Component  :标注一个普通的Spring Bean类 @Controller    :标注一个控制器组件器 @Service        :标注一个业务逻辑组件器 @Repository ...

  4. [20180926]共享池中的NETWORK BUFFER.txt

    [20180926]共享池中的NETWORK BUFFER.txt --//最近几天一直在探究SQL*Net more data from client 相关等待事件,发现SDU相关,自己也网上探究一 ...

  5. C#-枚举(十三)

    概念 枚举是一组命名整型常量 枚举类型是使用 enum 关键字声明的 例如定义一个变量,该变量的值表示一周中的一天: enum Days { Sunday, Monday, Tuesday, Wedn ...

  6. Failed to decrypt protected XML node "DTS:Password" with error 0x8009000B "Key not valid for use in specified state.". You may not be authorized to access this information. This error occurs when t

    Question SSIS包从A服务器搬迁到B服务器,运行报错 Description: Failed to decrypt protected XML node "DTS:Password ...

  7. Timeout occurred while waiting for latch: class 'ACCESS_METHODS_DATASET_PARENT'

    前些天某个SQL Server数据库的错误日志爆出如下错误: Timeout occurred while waiting for latch: class 'ACCESS_METHODS_DATAS ...

  8. 获取当前页面的URL信息

    以前在做网站的时候,经常会遇到当前页的分类高亮显示,以便让用户了解当前处于哪个页面.之前一直是在每个不同页面写方法.工程量大,也不便于修改.一直在想有什么简便的方法实现.后来在网上查到可以用获取当前U ...

  9. 16.ajax_case02

    # 抓取当当网书评 # http://product.dangdang.com/25340451.html import json import requests from lxml import e ...

  10. 自然语言处理之jieba分词

    在处理英文文本时,由于英文文本天生自带分词效果,可以直接通过词之间的空格来分词(但是有些人名.地名等需要考虑作为一个整体,比如New York).而对于中文还有其他类似形式的语言,我们需要根据来特殊处 ...