Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 递归问题中的原子整数或int[](LeetCode124)_Java_Recursion_Atomic - Fatal编程技术网

Java 递归问题中的原子整数或int[](LeetCode124)

Java 递归问题中的原子整数或int[](LeetCode124),java,recursion,atomic,Java,Recursion,Atomic,这就是LeetCode的问题124 我使用的Java没有全局变量,为什么我们需要使用int[]或atomic,但不能使用int来存储最大值? 我在这里缺少什么知识 public int maxGain(TreeNode currNode, int[] res) { if(currNode == null) { return 0; } int leftBestSum = Math.max(maxGain(currNode.left, res), 0);

这就是LeetCode的问题124 我使用的Java没有全局变量,为什么我们需要使用int[]或atomic,但不能使用int来存储最大值? 我在这里缺少什么知识

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];
    }

在引用的语言中,如C、C++、C、P、Perl等,可以将指针或引用传递给函数,并将其修改为:

#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;
}
输出:

复制原语:415
复制对对象的引用:42
在竞争性编程中,这是一种有用且可接受的技术,可以简化逻辑,但在生产环境中,这通常是一种懒惰的做法,因为它破坏了封装,使代码难以推理,并且具有语义成本


使用一个类来封装值,将函数放在一个范围内,这样就可以修改私有类成员,或者重写逻辑以避免需要引用。

< P>在引用的语言中,如C、C++、C、P、Perl等,可以将指针或引用传递给函数,并将其修改到位:

#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;
}
输出:

复制原语:415
复制对对象的引用:42
在竞争性编程中,这是一种有用且可接受的技术,可以简化逻辑,但在生产环境中,这通常是一种懒惰的做法,因为它破坏了封装,使代码难以推理,并且具有语义成本


使用一个类来封装值,将函数放在一个作用域中,这样它们就可以修改私有类成员,或者重写逻辑以避免引用。

这真是太麻烦了。您正试图通过使用容器作为输入变量来破解此解决方案,以允许输出参数。您将它放在一个容器中,以避免整数的值传递特性,这会将参数复制到函数中

相反,为什么不直接返回多个值呢

/**
 * 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];
}

这真是令人讨厌。您正试图通过使用容器作为输入变量来破解此解决方案,以允许输出参数。您将它放在一个容器中,以避免整数的值传递特性,这会将参数复制到函数中

相反,为什么不直接返回多个值呢

/**
 * 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];
}

Java是按值传递的。为了修改给定的原始整数,必须使用某种包装器。AtomicInteger和其他整数类型是有效的,数组选项也是有效的。为了修改给定的原始整数,必须使用某种包装器。AtomicInteger和其他整数类型是有效的,数组选项也是有效的。