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

資訊專欄INFORMATION COLUMN

[LintCode] Replace With Greatest From Right

xiaokai / 2957人閱讀

Problem

Given an array of integers, replace every element with the next greatest element (greatest element on the right side) in the array. Since there is no element next to the last element, replace it with -1. For example, if the array is [16, 17, 4, 3, 5, 2], then it should be modified to [17, 5, 5, 5, 2, -1].

Example

Give nums = [16, 17, 4, 3, 5, 2], change nums to [17, 5, 5, 5, 2, -1]
You should do it in place.

Solution
public class Solution {
    /**
     * @param nums: An array of integers.
     * @return: nothing
     */
    public void arrayReplaceWithGreatestFromRight(int[] nums) {
        // Write your code here.
        int n = nums.length-1;
        int max = nums[n];
        nums[n] = -1;
        for (int i = n-1; i >= 0; i--) {
            int cur = nums[i];
            nums[i] = max;
            max = Math.max(max, cur);
        }
        return;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/69465.html

相關(guān)文章

  • [LintCode/LeetCode] Lowest Common Ancestor of BST/

    摘要:遞歸左右子樹,若左右子樹都有解,那么返回根節(jié)點(diǎn)若僅左子樹有解,返回左子樹若僅右子樹有解,返回右子樹若都無解,返回。對(duì)于而言,更為簡(jiǎn)單公共祖先一定是大于等于其中一個(gè)結(jié)點(diǎn),小于等于另一個(gè)結(jié)點(diǎn)。 Problem Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the ...

    dinfer 評(píng)論0 收藏0
  • LintCode: Max Tree

    摘要:題目此題和很類似,所以第一反應(yīng)使用遞歸做。遞歸的解法過不了,會(huì)顯示超時(shí)比遞歸的更好的方法是用,比較難想到,代碼參考了思路是用一個(gè)單調(diào)遞減棧尋找最大值。這個(gè)操作可以幫我們順利找到左子樹和父節(jié)點(diǎn)。 題目 Given an integer array with no duplicates. A max tree building on this array is defined as fol...

    ivan_qhz 評(píng)論0 收藏0
  • LintCode Coins in a line III

    摘要:復(fù)雜度思路參考的思路,對(duì)于,表示在從到的范圍內(nèi),先手玩家能拿到的最大的硬幣價(jià)值。對(duì)于狀態(tài),先手玩家有兩種選擇,要么拿的硬幣,要么拿的硬幣左邊一個(gè)的或者右邊一側(cè)的,如果拿左側(cè)的硬幣,如果拿右側(cè)的硬幣,取兩個(gè)值的最大值。 LintCode Coins in a line III There are n coins in a line. Two players take turns to ...

    focusj 評(píng)論0 收藏0
  • [LintCode] Segment Tree Query I & Segment Tree

    摘要:題目是要查詢到這個(gè)區(qū)間內(nèi)某一點(diǎn)的。值是從最底層的子節(jié)點(diǎn)值里取最大值。因此,不用太復(fù)雜,遞歸就可以了。與所不同的是,對(duì)所給區(qū)間內(nèi)的元素個(gè)數(shù)求和,而非篩選。這樣就會(huì)出現(xiàn)的情況,視作本身處理。 Segment Tree Query Problem For an integer array (index from 0 to n-1, where n is the size of this ar...

    vibiu 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<