摘要:題目詳情給定一個(gè)整數(shù)數(shù)組,如果最多改變一個(gè)元素的值,就可使整個(gè)數(shù)組元素的值單調(diào)遞增。那么我們就返回,否則返回。
題目詳情
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).給定一個(gè)整數(shù)數(shù)組,如果最多改變一個(gè)元素的值,就可使整個(gè)數(shù)組元素的值單調(diào)遞增。那么我們就返回true,否則返回false。
Example 1:
Input: [4,2,3]
Output: True
Explanation: 把4改成1即可使數(shù)組單調(diào)遞增
Example 2:
Input: [4,2,1]
Output: False
Explanation: 不可能通過改變一個(gè)元素使數(shù)組單調(diào)遞增
首先設(shè)置一個(gè)count變量,統(tǒng)計(jì)array[i] > array[i+1],如果count大于1就返回false;
對于不符合條件的元素,我們要給他重新賦值使整個(gè)數(shù)組可以滿足單調(diào)遞增
解法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.
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/71169.html
摘要:前言從開始寫相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時(shí)也沒有按順序?qū)懍F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖?。所以在這里做個(gè)索引嘻嘻。順序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 從開始寫leetcode相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時(shí)也沒有按順序?qū)憽F(xiàn)在翻起來覺得蠻亂的??赡艽蠹铱粗卜浅2环奖恪K栽谶@里做個(gè)索引嘻嘻。 順序整理 1~50 1...
摘要:題目就是給一個(gè)數(shù)組,把每一項(xiàng)的對應(yīng)的組合成一個(gè)新的數(shù)組,再算出那些不是遞增的個(gè)數(shù)。例子我的算法其他算法思路跟我的差不多,只不過用了的 題目 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...
摘要:題目就是給一個(gè)數(shù)組,把每一項(xiàng)的對應(yīng)的組合成一個(gè)新的數(shù)組,再算出那些不是遞增的個(gè)數(shù)。例子我的算法其他算法思路跟我的差不多,只不過用了的 題目 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...
摘要:題目示例一示例二注意自己的解法其他解法 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...
摘要:題目示例一示例二注意自己的解法其他解法 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...
閱讀 2402·2021-11-24 10:26
閱讀 2589·2021-11-16 11:44
閱讀 1707·2021-09-22 15:26
閱讀 3588·2021-09-10 11:11
閱讀 3195·2021-09-07 10:25
閱讀 3635·2021-09-01 10:41
閱讀 1018·2021-08-27 13:11
閱讀 3516·2021-08-16 11:02