Leetcode665.Non-decreasing Array非递减数组
给定一个长度为 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非递减数组的更多相关文章
- [LeetCode] Non-decreasing Array 非递减数列
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- 665. Non-decreasing Array只允许修改一位数的非递减数组
[抄题]: Given an array with n integers, your task is to check if it could become non-decreasing by mod ...
- 顺序表习题(1)-打印非递减数组a与b的升序并集(去除重复元素)
void Print_Union(SqList a,SqList b) { , q = ; //初始化指针 ; //记录上一次打印的元素 while (p!=a.length&&q!= ...
- [Swift]LeetCode665. 非递减数列 | Non-decreasing Array
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- LeetCode 665. 非递减数列(Non-decreasing Array)
665. 非递减数列 665. Non-decreasing Array 题目描述 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是 ...
- HDU 5532 Almost Sorted Array (最长非递减子序列)
题目链接 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap s ...
- 【LeetCode】665. 非递减数列 Non-decreasing Array(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:数组,array,非递减,遍历,python,C++ 目录 题目描述 题目大意 解题方法 一.错误代码 二.举例分析 ...
- 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 665.非递减数列
非递减数列 给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i ...
随机推荐
- 我喜欢Mouding
我Smily喜欢Mouding
- PAT甲级——A1080 Graduate Admission
It is said that in 2011, there are about 100 graduate schools ready to proceed over 40,000 applicati ...
- 群晖系统下btfs(占用5%)和ext4文件格式的硬盘占用和选择
1个人感觉选择ext4就可以,毕竟重要数据会设置raid1 不重要的数据也不需要快照什么的 商用除外 对于一个4t的硬盘 实际容量3726G 使用ext4可用空间3666G,群晖 占用60G大概1.6 ...
- 03_springmvc整合mybatis
一.整合思路 springmvc+mybaits的系统架构: 第一步整合dao层:mybatis和spring整合:通过spring管理mapper接口,使用mapper的扫描器自动扫描mapper接 ...
- 坐标转换,EPSG:4326转换成高德坐标教程
这里先给大家介绍几个坐标系: 1.WGS84:国际坐标系,为一种大地坐标系,也是目前广泛使用的GPS全球卫星定位系统使用的坐标系.2.GCJ02:火星坐标系,是由中国国家测绘局制订的地理信息系统的坐标 ...
- 运行第一个python程序,python 变量,常量,注释
一.运行第一个python程序: print('Hello,world') 保存为.py文件 在cmd窗口: python3x:python py文件路径 回车 python2x:python p ...
- css3之2D 转换
浏览器支持 表格中的数字表示支持该属性的第一个浏览器版本号. 紧跟在 -webkit-, -ms- 或 -moz- 前的数字为支持该前缀属性的第一个浏览器版本号. Chrome 和 Safari 要求 ...
- css 超出两行省略号,超出一行省略号
参考:https://www.cnblogs.com/yangguojin/p/10301981.html 超出一行省略: p{ white-space:nowrap; overflow:hidden ...
- 【转载:java】详解java中的注解(Annotation)
目录结构: contents structure [+] 什么是注解 为什么要使用注解 基本语法 4种基本元注解 重复注解 使用注解 运行时处理的注解 编译时处理的注解 1.什么是注解 用一个词就可以 ...
- vue:解决使用param传参后,再次刷新页面会新增一个原有的tab
问题:在最近的项目中,我通过传递不同的参数,复用同一组件进行渲染,然而意外出现一个bug,就是当我重新刷新该页面时,会新增一个tab 原来的: 刷新页面后: 查阅资料后,发现该现象是由于通过param ...