成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

leetcode 665 Non-decreasing Array

hoohack / 2683人閱讀

摘要:題目詳情給定一個(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)文章

  • leetcode 部分解答索引(持續(xù)更新~)

    摘要:前言從開始寫相關(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...

    leo108 評論0 收藏0
  • 10.leetcode Delete Columns to Make Sorted

    摘要:題目就是給一個(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...

    littlelightss 評論0 收藏0
  • 10.leetcode Delete Columns to Make Sorted

    摘要:題目就是給一個(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...

    brianway 評論0 收藏0
  • 4. leetcode 數(shù)組平方和的排序

    摘要:題目示例一示例二注意自己的解法其他解法 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 數(shù)組平方和的排序

    摘要:題目示例一示例二注意自己的解法其他解法 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

發(fā)表評論

0條評論

閱讀需要支付1元查看
<