LeetCode 665. Non-decreasing Array (不递减数组)
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: True
Explanation: You could modify the first4
to1
to get a non-decreasing array.
Example 2:
Input: [4,2,1]
Output: False
Explanation: You can't get a non-decreasing array by modify at most one element.
Note: The n
belongs to [1, 10,000].
题目标签:Array
题目给了我们一个nums array, 只允许我们一次机会去改动一个数字,使得数组成为不递减数组。可以实现的话,return true;不行的话,return false。
这个题目关键在于,当遇见一个 nums[i] > nums[i+1] 的情况,我们是把 nums[i]降为nums[i+1] 还是 把nums[i+1]升为nums[i]。
如果可行的话,当然是选择优先把 nums[i]降为nums[i+1],这样可以减少 nums[i+1] > nums[i+2] 的风险。
来看一下两种情况:
a. 1 3 5 4 6 7 --> 1 3 4 4 6 7
当遇到5 > 4 的情况,这里因为4比5 之前的所有数字都大,所以可以把5 降为4。
b. 1 4 5 3 6 7 --> 1 4 5 5 6 7
当遇到5 > 3 的情况,这里3比5之前的4小,所以没有选择,只能把3 升为5。
当需要第二次改动的时候,可以直接返回false,不需要把剩下的array走完。
Java Solution:
Runtime beats 89.68%
完成日期:10/20/2017
关键词:Array
关键点:了解有2种改动情况和优先级
class Solution
{
public boolean checkPossibility(int[] nums)
{
boolean modified = false; for(int i=0; i<nums.length; i++)
{
if(i+1 < nums.length && nums[i] > nums[i+1])
{
if(modified) // if modified a number already
return false;
else // if it is first time to modify a number
{
if(i-1 < 0 || nums[i+1] >= nums[i-1]) // if nums[i+1] is larger or equal all numbers before nums[i]
nums[i] = nums[i+1]; // change nums[i] as same as nums[i+1]
else // if nums[i+1] is not larger than all numbers before nums[i]
nums[i+1] = nums[i]; // change nums[i+1] as same as nums[i] modified = true;
}
}
} return true; }
}
参考资料:N/A
LeetCode 题目列表 - LeetCode Questions List
LeetCode 665. Non-decreasing Array (不递减数组)的更多相关文章
- [LeetCode] 88. Merge Sorted Array 合并有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- Leetcode665.Non-decreasing Array非递减数组
给定一个长度为 n 的整数数组,你的任务是判断在最多改变 1 个元素的情况下,该数组能否变成一个非递减数列. 我们是这样定义一个非递减数列的: 对于数组中所有的 i (1 <= i < n ...
- 【LeetCode】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...
- LeetCode OJ:Rotate Array(倒置数组)
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- [leetcode]88. Merge Sorted Array归并有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- [LeetCode] Non-decreasing Array 非递减数列
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- [LeetCode] Longest Mountain in Array 数组中最长的山
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length ...
- LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组)
LeetCode初级算法--设计问题01:Shuffle an Array (打乱数组) 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:h ...
- LeetCode算法题-Non-decreasing Array(Java实现)
这是悦乐书的第283次更新,第300篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第151题(顺位题号是665).给定一个包含n个整数的数组,您的任务是通过修改最多1个元 ...
随机推荐
- Thinkphp3.2结合phpqrcode生成二维码(含Logo的二维码),附案例
首先,下载phpqrcode,将其解压到项目ThinkPHP\Library\Vendor目录下.Index_index.html(模板可自行配置) <form action="{:U ...
- python读取外部文件
>>> pd.read_excel('c://111.xlsx') 年度排名 历史排名 电影名称 总票房 总人次 总场次 上映年份 操作 0 1 1 美人鱼 NaN -- -- 20 ...
- java基础知识5--集合类(Set,List,Map)和迭代器Iterator的使用
写的非常棒的一篇总结: http://blog.csdn.net/speedme/article/details/22398395#t1 下面主要看各个集合如何使用迭代器Iterator获取元素: 1 ...
- Linux配置SSH端口以及密钥登录
改端口后重启: vim /etc/ssh/sshd_config systemctl restart sshd
- springboot+swagger2
springboot+swagger2 小序 新公司的第二个项目,是一个配置管理终端机(比如:自动售卖机,银行取款机)的web项目,之前写过一个分模块的springboot框架,就在那个框架基础上进行 ...
- js 第一课
什么是JavaScript JavaScript是一种脚本语言,运行在网页上.无需安装编译器.只要在网页浏览器上就能运行 一般JavaScript与HTML合作使用. 例如 <html> ...
- Redis的安装以及在项目中使用Redis的一些总结和体会
第一部分:为什么我的项目中要使用Redis 我知道有些地方没说到位,希望大神们提出来,我会吸取教训,大家共同进步! 注册时邮件激活的部分使用Redis 发送邮件时使用Redis的消息队列,减轻网站压力 ...
- 【框架学习与探究之定时器--Quartz.Net 】
声明 本文欢迎转载,原文地址:http://www.cnblogs.com/DjlNet/p/7572174.html 前言 这里相信大部分玩家之前现在都应该有过使用定时器的时候或者需求,例如什么定时 ...
- C/C++ 进程通讯(命名管道)
服务端代码: // pipe_server.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdio.h> ...
- android studio集成ijkplayer
介绍 ijkplayer是一款非常火的开源视频播放器,android和IOS通用.关于怎么编译怎么导入android Studio中自己的项目,其中坑很多,本篇记录下自己的操作记录.ijkplayer ...