665. Non-decreasing Array - LeetCode
Question
Solution
题目大意:
思路:当前判断2的时候可以将当前元素2变为4,也可以将上一个元素4变为2,再判断两变化后是否满足要求。
Java实现:
public boolean checkPossibility(int[] nums) {
if (nums == null || nums.length < 3) return true;
int count = 0;
// 判断前2个
if (nums[1] < nums[0]) {
nums[0] = nums[1] - 1;
count++;
}
for (int i = 2; i < nums.length; i++) {
if (nums[i] < nums[i - 1]) {
count++;
if (nums[i - 2] <= nums[i] - 1) {
nums[i - 1] = nums[i] - 1;
} else if (i == nums.length -1 || nums[i + 1] >= nums[i - 1] + 1) {
nums[i] = nums[i - 1] + 1;
} else {
return false;
}
}
}
return count < 2;
}
别人实现:
public boolean checkPossibility(int[] nums) {
int cnt = 0; //the number of changes
for(int i = 1; i < nums.length && cnt<=1 ; i++){
if(nums[i-1] > nums[i]){
cnt++;
//modify nums[i-1] of a priority
if(i-2<0 || nums[i-2] <= nums[i])nums[i-1] = nums[i];
else nums[i] = nums[i-1]; //have to modify nums[i]
}
}
return cnt<=1;
}
665. Non-decreasing Array - LeetCode的更多相关文章
- Find Minimum in Rotated Sorted Array leetcode
原题链接 直接贴代码,这道题是 search in rotated sorted array leetcode 的前面部分! class Solution { public: int findMin( ...
- Find First and Last Position of Element in Sorted Array - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Find First and Last Position of Element in Sorted Array - LeetCode 注意点 nums可能 ...
- 697. Degree of an Array - LeetCode
697. Degree of an Array - LeetCode Question 697. Degree of an Array - LeetCode Solution 理解两个概念: 数组的度 ...
- Kth Largest Element in an Array - LeetCode
examination questions Find the kth largest element in an unsorted array. Note that it is the kth lar ...
- Merge Sorted Array——LeetCode
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
随机推荐
- 《深入理解ES6》笔记——扩展对象的功能性(4)
变量功能被加强了.函数功能被加强了,那么作为JavaScript中最普遍的对象,不加强对得起观众吗? 对象类别 在ES6中,对象分为下面几种叫法.(不需要知道概念) 1.普通对象 2.特异对象 3.标 ...
- 【面试普通人VS高手系列】谈谈你对AQS的理解
AQS是AbstractQueuedSynchronizer的简称,是并发编程中比较核心的组件. 在很多大厂的面试中,面试官对于并发编程的考核要求相对较高,简单来说,如果你不懂并发编程,那么你很难通过 ...
- Android限制输入框内容
<EditText android:id="@+id/temper" android:hint="36.2" android:digits="1 ...
- 一篇文章带你整明白HTTP缓存知识
最近看了很多关于缓存的文章, 每次看完,看似明白但是实际还是没明白,这次总算搞明白协商缓存是怎么回事了 首先,服务器缓存分强制缓存和协商缓存(也叫对比缓存) 强制缓存一般是服务端在请求头携带字段Exp ...
- JdGrid排序问题
JdGrid排序问题 js代码 function gridList() { var $gridList = $("#gridList"); $gridList.dataGrid({ ...
- 在定义C++, C通用接口函数时让C++接口支持默认参数
在SOUI4的开发中,所有SOUI核心对象都采用了一种类似COM接口的技术来导出接口. 这所以采用这种方案,主要目的是为了让SOUI4支持C语言调用,扩展SOUI的使用场景. 众所周知,C++函数的参 ...
- 7.Docker容器使用辅助工具汇总
原文地址: 点击直达 more information: https://docs.docker.com/engine/security/security/#docker-daemon-attack- ...
- Java学习day6
今天跟着教学视频做了个简易的学生管理系统 在编写完全部代码之后出现了在空白处右键没有run as选项的问题,通过csdn与博客园上的多个帖子介绍,得知是jdk配置不对,正确配置后问题得到解决 明天学习 ...
- 微信授权 - wx.openSetting
wx.openSetting({ // 唤醒授权页面 success: res => { console.log('res',res) // 授权成功操作 }, ...
- Metalama简介4.使用Fabric操作项目或命名空间
使用基于Roslyn的编译时AOP框架来解决.NET项目的代码复用问题 Metalama简介1. 不止是一个.NET跨平台的编译时AOP框架 Metalama简介2.利用Aspect在编译时进行消除重 ...