博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 665. Non-decreasing Array(Easy)
阅读量:7193 次
发布时间:2019-06-29

本文共 1346 字,大约阅读时间需要 4 分钟。

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: TrueExplanation: You could modify the first 4 to 1 to get a non-decreasing array.

 

Example 2:

Input: [4,2,1]Output: FalseExplanation: 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, 33, 4, 2, 3判断当前的值是否比前一个值小,如果小的话, 再判断是否比 前前一个值 要小, 如果小,改变当前的值 使得其跟前一个值一样大,如果大,改变前一个值,使得它跟当前的值一样大。*/class Solution {public:    bool checkPossibility(vector
& nums) { int len = nums.size(); int count = 0; // if (len == 0) return false; for (int i = 1; i < len; i++){ if (nums[i] < nums[i - 1]){ count ++; if (count > 1){ return false; } if (nums[i] < nums[i - 2] && (i - 2) >= 0){ nums[i] = nums[i - 1]; } else{ nums[i - 1] = nums[i]; } } } return true; }};

 

转载于:https://www.cnblogs.com/simplepaul/p/7755571.html

你可能感兴趣的文章
nancy框架hosting.self
查看>>
PHP问题 —— The use statement with non-compound name
查看>>
xoda 0.4.6在windows下搜索功能
查看>>
SSL 域名证书 安装指引
查看>>
专题:Android 移植到C#
查看>>
Subtitle Editor:适合字幕编辑但缺少说明
查看>>
C++11新特性中的匿名函数Lambda表达式的汇编实现分析(一)
查看>>
一看就会的SHELL语法
查看>>
DbVisualizer中jdk的设置
查看>>
selenium webdriver(6)—cookie相关操作
查看>>
VMware虚拟机转VitrualBOX虚拟机等后续优化操作
查看>>
三次握手 四次挥手
查看>>
Android手机中的“秘码”
查看>>
使用Redis中hscan的坑
查看>>
quartz 使用 PostgreSQL抛错“不良的类型值: long : \x
查看>>
Python的getattr(),setattr(),delattr(),hasattr()
查看>>
linux命令学习——tar
查看>>
Go语言数据结构
查看>>
linux常用命令
查看>>
jQuery 1.6 中文API (适用jQuery 1.6和jQuery 1.6.1)
查看>>