资讯专栏INFORMATION COLUMN

leetcode 665 Non-decreasing Array

hoohack / 2521人阅读

摘要:题目详情给定一个整数数组,如果最多改变一个元素的值,就可使整个数组元素的值单调递增。那么我们就返回,否则返回。

题目详情
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).

给定一个整数数组,如果最多改变一个元素的值,就可使整个数组元素的值单调递增。那么我们就返回true,否则返回false。

Example 1:
Input: [4,2,3]
Output: True
Explanation: 把4改成1即可使数组单调递增
Example 2:
Input: [4,2,1]
Output: False
Explanation: 不可能通过改变一个元素使数组单调递增

想法

首先设置一个count变量,统计array[i] > array[i+1],如果count大于1就返回false;

对于不符合条件的元素,我们要给他重新赋值使整个数组可以满足单调递增

解法
Example 1:

Input: [4,2,3]
Output: True
Explanation: You could modify the first 4 to 1 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.

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/71169.html

相关文章

  • leetcode 部分解答索引(持续更新~)

    摘要:前言从开始写相关的博客到现在也蛮多篇了。而且当时也没有按顺序写现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。顺序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 从开始写leetcode相关的博客到现在也蛮多篇了。而且当时也没有按顺序写~现在翻起来觉得蛮乱的。可能大家看着也非常不方便。所以在这里做个索引嘻嘻。 顺序整理 1~50 1...

    leo108 评论0 收藏0
  • 10.leetcode Delete Columns to Make Sorted

    摘要:题目就是给一个数组,把每一项的对应的组合成一个新的数组,再算出那些不是递增的个数。例子我的算法其他算法思路跟我的差不多,只不过用了的 题目 We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indice...

    littlelightss 评论0 收藏0
  • 10.leetcode Delete Columns to Make Sorted

    摘要:题目就是给一个数组,把每一项的对应的组合成一个新的数组,再算出那些不是递增的个数。例子我的算法其他算法思路跟我的差不多,只不过用了的 题目 We are given an array A of N lowercase letter strings, all of the same length. Now, we may choose any set of deletion indice...

    brianway 评论0 收藏0
  • 4. leetcode 数组平方和的排序

    摘要:题目示例一示例二注意自己的解法其他解法 1. 题目Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.示例一: Input: [-4,-1,0,3,10...

    zhangke3016 评论0 收藏0
  • 4. leetcode 数组平方和的排序

    摘要:题目示例一示例二注意自己的解法其他解法 1. 题目Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.示例一: Input: [-4,-1,0,3,10...

    张宪坤 评论0 收藏0

发表评论

0条评论

阅读需要支付1元查看
<