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 ...
随机推荐
- 《CSS 揭秘》作者Lea Verou:我喜欢分享开源的行业文化
本文仅用于学习和交流,不用于商业目的.非商业转载请注明作译者.出处,并保留本文的原始链接:http://www.ituring.com.cn/art... 访谈嘉宾: Lea VerouW3C CSS ...
- React系列——websocket群聊系统在react的实现
前奏 这篇文章仅对不熟悉在react中使用socket.io的人.以及websocket入门者有帮助. 下面这个动态图展示的聊天系统是用react+express+websocket搭建的,很模糊吧, ...
- D2Admin 8月更新: 高级数据持久化|标签页右键|模块化等
剧透:这次,D2Admin 带来了其它同类模板都没有的"花式"数据持久化功能,以及极少同类产品才有的标签页右键控制... 概述 D2Admin 7月份更新到了 1.1.5 版本 相 ...
- 前端存储 - localStorage
发布自Kindem的博客,欢迎大家转载,但是要注意注明出处 localStorage 介绍 在HTML5中,引入了两个新的前端存储特性: localStorage sessionStorage 这两者 ...
- 前端面试题整理——Javascript基础
常见值类型: let a; //undefined let s = 'abc'; let n = 100; let b = true; let sb = Symbol('s'); let nn = N ...
- java中类变量和实例变量的实质区别?
类变量和实例变量的区别 相对于static(静态的)或说类的, 本章开始提到的都是instance(实例的)或说对象的. 每个对象都有自己的一份儿对象域或实例域,相互之间没关系, 不共享. 我们可以从 ...
- Virtual Function(虚函数)in c++
Virtual Function(虚函数)in c++ 用法: virtual void log() { std::cout << "hello world!" < ...
- 什么是机器学习的分类算法?【K-近邻算法(KNN)、交叉验证、朴素贝叶斯算法、决策树、随机森林】
1.K-近邻算法(KNN) 1.1 定义 (KNN,K-NearestNeighbor) 如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类 ...
- 【课程汇总】OpenHarmony 成长计划知识赋能第二期课程(附链接)
OpenHarmony 开源开发者成长计划第二期知识赋能直播课程以入门为主,共设置 8 节课,覆盖了应用开发.设备开发.内核驱动等多个技术领域.带领开发者快速了解如何玩转 OpenHarmony.如何 ...
- 前端vue之属性指令、style和class、条件渲染、列表渲染、事件处理、数据双向绑定、表单控制、v-model进阶
今日内容概要 属性指令 style和class 条件渲染 列表渲染 事件处理 数据的双向绑定 v-model进阶 购物车案例 内容详细 1.属性指令 <!DOCTYPE html> < ...