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 ...
随机推荐
- Python学习—(windows系统下)安装pygame
浏览器搜索pygame的windows安装程序,下载与python版本相匹配的文件. 如果.exe文件直接运行: 如果.whl文件,将其复制到要用到的项目文件夹中,在cmd窗口中切换到该文件所在的文件 ...
- PCB常用低速、高速板材参数性能(2)
- TOGAF D阶段:技术架构
11. Phase D: Technology Architecture (opengroup.org) Phase D: Technology Architecture D阶段:技术架构 11.1 ...
- html5 canvas基础10点
本文主要讲解下一些canvas的基础 1.<canvas id="canvas">若此浏览器不支持canvas会显示该文字</canvas> //创建个ht ...
- 根据地理信息绘画的html5 小游戏 - 简单实现
好久没写文章了,之前一直有一个想法,就是做一个根据用户行走的路线,获取地理位置,然后把它们绘制出来,最后产生的效果,类似蜗牛行走留下的痕迹. 最近思考了一下,搭了一个https,简单实现了一下,提供一 ...
- Java习题
public class ClassTest{ String str = new String("hello"); char[] ch = {'a','b','c'}; publi ...
- Struts2-获取值栈对象与结构
1.获取值栈对象有多种方式 (1)使用ActionContext类里面的方法获取值栈对象 @Override public String execute(){ //1.获取ActionContext类 ...
- SpringMVC-开启静态资源访问权限
1.配置 <mvc:resources mapping="/js/**" location="/js/"/> mapping:代表js目录下的所有文 ...
- 项目-MyBlog
项目 地址:https://gitee.com/zwtgit/my-blog 由Docker + SpringBoot2.0 + Mybatis + thymeleaf 等技术实现, 功能齐全.部署简 ...
- C++的"开始" Hello World! 你好世界!
# C++的"开始" Hello World! 你好世界! ```C++ // 第一个程序 //代表注释这一行 #include <iostream> //c++专属头 ...