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 ...
随机推荐
- 使用Node.js版本管理器
使用Node.js版本管理器 完全卸载Node.js 清除Package缓存:npm cache clean --force 卸载Node.js:wmic product where caption= ...
- 从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
回顾 上一节我们搭建了游戏的骨架,添加了四个游戏场景,分别是加载.开始.游戏.结束.那么这一节我们来介绍加载这个场景,顺带丰富一下各个场景的基本内容. Phaser.Loader Phaser框架自带 ...
- 关于表达式&& 和 || 有多项的时候的取值
&& 表达式只有两项的时候,如果表达式为false, 返回为false 的那一个 ,为true的时候 返回最后一个值 || 只有两项的时候,返回为true 的那一个;都为fal ...
- vue里面v-for显示红色波浪线
vue里面使用v-for代码显示红色的波浪线,解决办法: before: <div v-for="tmsgs in msg.message"></div> ...
- Java 实例 - 读取文件内容
原文作者:菜鸟教程 原文链接:Java 实例 - 读取文件内容(建议前往原文以获得最佳体验) 按行读取文本文件 import java.io.*; public class Main { public ...
- Git---git的常用操作
git三种状态的转换 git状态切换时的常用命令 1. git管理工作目录 git init # 会增加.git文件夹 2. git的三种状态 工作区 暂存区 本地仓库 3. 提交到暂存区 git a ...
- 3.Docker容器学习之新手基础使用
原文地址: http://blog.weiyigeek.top/2019/5/2-docker%E5%AD%A6%E4%B9%A0%E4%B9%8B%E5%9F%BA%E7%A1%80%E4%BD%B ...
- JavaWeb学习day3-Maven&安装
1.官网下载:https://maven.apache.org/ 2.解压下载好的压缩包 3.配置环境变量 添加如下图变量 在path变量下添加下图 4.安装完成检测 cmd输入:mvn -versi ...
- Codeforces Round #710 (Div. 3) Editorial 1506A - Strange Table
题目链接 https://codeforces.com/contest/1506/problem/A 原题 1506A - Strange Table Example input 5 1 1 1 2 ...
- 用 DOM 获取页面的元素方法集合
document.getElementById('id名') // 获取页面设置指定 id 的元素 document.getElementsByTagName('标签名') ...