第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

遞歸問(wèn)題中的原子整數(shù)或 int[] (LeetCode124)

遞歸問(wèn)題中的原子整數(shù)或 int[] (LeetCode124)

汪汪一只貓 2023-02-16 17:02:40
所以這是 LeetCode 第 124 題 我使用沒(méi)有全局變量的 Java,為什么我們需要使用 int[] 或 atomic 但不能使用 int 來(lái)存儲(chǔ)最大值?我在這里缺乏什么知識(shí)?public int maxGain(TreeNode currNode, int[] res) {        if(currNode == null) { return 0; }        int leftBestSum = Math.max(maxGain(currNode.left, res), 0);        int rightBestSum = Math.max(maxGain(currNode.right, res), 0);        // update best result if it's better to start a new path        int currNodeAndChildSum = currNode.val + leftBestSum + rightBestSum;        // if currSum is better than the best result, start new path        res[0] = Math.max(res[0], currNodeAndChildSum);        // else if currSum is not better than the best result, pass back the best result path to        // its parent for later compare use        int currBestPathSum = currNode.val + Math.max(leftBestSum, rightBestSum);        return currBestPathSum;    }    public int maxPathSum(TreeNode root) {        int[] res = new int[] {Integer.MIN_VALUE};        maxGain(root, res);        return res[0];    }
查看完整描述

2 回答

?
翻翻過(guò)去那場(chǎng)雪

TA貢獻(xiàn)2065條經(jīng)驗(yàn) 獲得超14個(gè)贊

在 C、C++、C#、PHP、Perl 等具有引用的語(yǔ)言中,您可以將指針或?qū)χ档囊脗鬟f給函數(shù)并就地修改它:


#include <stdio.h>


void doit(int *ptr_to_n) {

    *ptr_to_n = 42; // modifies the value pointed to by ptr_to_n by a dereference

}


int main(void) {

    int n = 415;

    doit(&n);

    printf(" %d\n", n); // => 42

    return 0;

}

Java 是嚴(yán)格按值傳遞的,不提供指針或?qū)ぶ愤\(yùn)算符。您在此處提供的代碼濫用數(shù)組來(lái)克服此限制。在下面的代碼中,調(diào)用范圍中對(duì)數(shù)組的引用被復(fù)制并按值傳遞給res,但res仍然指向與 相同的底層對(duì)象arr,因此數(shù)組本身沒(méi)有被復(fù)制,只是被別名化了。


class Main {

    public static void main(String[] args) {

        int n = 415;

        int[] arr = {415};

        doit(n);

        doitPointer(arr);


        System.out.println("Copy primitive: " + n);

        System.out.println("Copy reference to object: " + arr[0]);

    }


    static void doit(int n) {

        n = 42; // assignment to local variable which will be unavailable in outer scope

    }


    static void doitPointer(int[] res) {

        res[0] = 42; // assignment to element in referenced array, available in outer scope

    }

}

輸出:


Copy primitive: 415

Copy reference to object: 42

在競(jìng)爭(zhēng)性編程中,這是一種可以簡(jiǎn)化邏輯的有用且可接受的技術(shù),但通常這在生產(chǎn)環(huán)境中是懶惰的做法,因?yàn)樗茐牧朔庋b,使代碼難以推理并具有語(yǔ)義成本。


使用一個(gè)類(lèi)來(lái)封裝值,將函數(shù)放在一個(gè)范圍內(nèi),以便它們可以修改私有類(lèi)成員,或者重寫(xiě)邏輯以避免引用的需要。


查看完整回答
反對(duì) 回復(fù) 2023-02-16
?
aluckdog

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個(gè)贊

這真的很hacky。您正試圖通過(guò)使用容器作為輸入變量來(lái)破解此解決方案以允許輸出參數(shù)。您將它放在一個(gè)容器中以避免整數(shù)的按值傳遞性質(zhì),這會(huì)將參數(shù)復(fù)制到函數(shù)中。


相反,為什么不只返回多個(gè)值呢?


/**

 * Return an array containing the overall nodes 

 * maximum height and the overall max gain.

 **/

public int[] maxGain(TreeNode currNode) {

    if (currNode == null) {

        return new int[] { 0, Integer.MIN_VALUE };

    }


    int[] leftResult = maxGain(currNode.left);

    int[] rightResult = maxGain(currNode.right);


    int maxHeight = Math.max(leftResult[0], rightResult[0]) + currNode.val;


    int currGain = leftResult[0] + rightResult[0] + currNode.val;


    int maxGain = Math.max(currGain, Math.max(leftResult[1], rightResult[1]));


    return new int[] { maxHeight, maxGain };

}


public int maxPathSum(TreeNode root) {

    int[] result = maxGain(root);

    return result[1];

}


查看完整回答
反對(duì) 回復(fù) 2023-02-16
  • 2 回答
  • 0 關(guān)注
  • 100 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)