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 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 first4to1to 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)的更多相关文章
- Leetcode 88. Merge Sorted Array(easy)
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- 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 ...
- [LeetCode] 038. Count and Say (Easy) (C++/Python)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 038. Cou ...
- [array] leetCode-26. Remove Duplicates from Sorted Array - Easy
26. Remove Duplicates from Sorted Array - Easy descrition Given a sorted array, remove the duplicate ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- LeetCode 665. 非递减数列(Non-decreasing Array)
665. 非递减数列 665. Non-decreasing Array 题目描述 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是 ...
- LeetCode 665. Non-decreasing Array (不递减数组)
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- leetcode 448. Find All Numbers Disappeared in an Array -easy (重要)
题目链接: https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/description/ 题目描述: Give ...
随机推荐
- ERROR 1050 (42S01): Table xxx already exists
今天遇到一个关于MySQL求助的问题,修改表结构时遇到"ERROR 1050 (42S01): table xxx already exits" mysql> ALTER ...
- 安卓程序中手机后退键与标题栏后退键是不同的,前者回出发onBackPressed()函数,后者需要重重写temclick函数
安卓程序中手机后退键与标题栏后退键是不同的,前者回出发onBackPressed()函数,后者需要重重写temclick函数
- Cas 服务器 Service(Cas客户端)注册信息维护
作为Cas服务器,允许哪些客户端接入与否是通过配置来定义的.对Cas服务器来说,每一个接入的客户端与一个Service配置对应:在Cas服务器启动时加载并注册上这些Service,与之对应的客户端才能 ...
- [20190321]smem的显示缺陷.txt
[20190321]smem的显示缺陷.txt1.smem 加入-m参数显示存在缺陷,map的信息不全:# smem -tk -m -U oracle -P "oraclepeis|ora_ ...
- web前端(6)—— 标签的属性,分类,嵌套
属性 HTML标签可以设置属性,属性一般以键值对的方式写在开始标签中 1.HTML标签除一些特定属性外可以设置自定义属性,一个标签可以设置多个属性用空格分隔,多个属性不区分先后顺序. 2.属性值要用引 ...
- 简单易懂的程序语言入门小册子(7):基于文本替换的解释器,加入continuation,重构解释器
或许在加入continuation之前要先讲讲费这么大劲做这个有什么意义. 毕竟用不用continuation的计算结果都是一样的. 不过,这是一个兴趣使然的系列,学习这些知识应该完全出于好奇与好玩的 ...
- c/c++ 友元基本概念
友元基本概念: 1,把一个一般函数声明为一个类的友元函数 2,把一个类A的某几个成员函数声明为某个类B的友元函数 3,把一个类A声明为一个类B的友元类. 友元的作用:可以访问类B里所有的成员变量和成员 ...
- xshell的一些常用配置
1 在xshell中鼠标滚轮和右键的快捷方式 利用鼠标滚轮和右键快速粘贴上面复制的内容 打开xshell的工具---选项-----键盘和鼠标 点击确定 回到xshell即可使用
- iOS弹出UIViewController小视图
在TestViewController1中弹出TestViewController2 在TestViewController中点击按钮或者什么触发方法里面写入以下代码 TestViewControll ...
- python mysql数据库操作
一.pymysql 模块安装(本文博客推荐:https://www.cnblogs.com/clschao/articles/10023248.html) pip3 install pymysql 二 ...