小编Cha*_*ran的帖子

尝试使用两个if语句打印树的顶视图

问题陈述

您将获得指向二叉树根的指针.打印二叉树的顶视图.您只需要完成该功能.

我的代码:

void top_view(Node root)
 {  
       Node r = root;

       if(r.left!=null){
          top_view(r.left);
          System.out.print(r.data + " ");
        }
       if(r.right!=null){
          System.out.print(r.data + " ");
          top_view(r.right);
        }
}
Run Code Online (Sandbox Code Playgroud)

每次调用函数时都会执行两个if语句,但我只需要执行其中一个.我尝试过切换但是它给出了常量表达式错误.我已经为这个问题找到了不同的解决方案.

所以我只想知道如果一次执行我们是否只能制作一个,即有没有办法在不改变方法的情况下修复我的代码?

在此输入图像描述 在此输入图像描述

问题链接: https ://www.hackerrank.com/challenges/tree-top-view

java treeview tree binary-tree data-structures

13
推荐指数
2
解决办法
8909
查看次数

从同一个数组创建两个列表,修改一个列表,更改另一个列表

我从同一个数组创建了两个列表并对其中一个进行了排序.当我尝试更改一个列表时,其他列表也已更新.

List<Integer> list = Arrays.asList(ar);
List<Integer> sorted = Arrays.asList(ar);
Collections.sort(sorted);
list.set(0,10000000); //changes sorted also
Run Code Online (Sandbox Code Playgroud)

我花了一段时间才弄明白,下面提到的代码工作了.

List<Integer> sorted = new ArrayList<Integer>(Arrays.asList(ar));

我想知道为什么我的第一种方法不起作用?我创建了两个单独的列表,为什么这两个列表都发生了变化.java如何在这里为变量赋值?

java arrays java-8

8
推荐指数
1
解决办法
1021
查看次数

为什么需要使用"big = big.add(..)"来对BigIntegers求和?

我是初学者.这可能是一个愚蠢的问题.

我有一大堆非常大的数字.我需要找到数组中所有这些数字的总和.我定义了一个BigInteger并将其初始化为零.现在我将遍历数组并将每个元素添加到此BigInteger.

BigInteger big = BigInteger.ZERO;
for(BigInteger b : array){
   big.add(b);
}
Run Code Online (Sandbox Code Playgroud)

没有编译错误但big值仍然为零,代码不起作用.所以,我检查了一下并学习了BigInteger add方法返回的总和.我修改了上面的代码.

big = big.add(b);
Run Code Online (Sandbox Code Playgroud)

现在这很好用.

我的问题:那里到底发生了什么?为什么没有第一个代码更新big值.

我可以比较这BigInteger.add()collection.add()

感谢更多的见解.谢谢.

java arrays add biginteger

5
推荐指数
1
解决办法
461
查看次数

从命令行自动配置 Jupyter 密码

我了解到 Jupyter-notebook 可以配置密码而不是令牌。

两步——

$ jupyter notebook password
Enter password:  ****
Verify password: ****
[NotebookPasswordApp] Wrote hashed password to /Users/you/.jupyter/jupyter_notebook_config.json
Run Code Online (Sandbox Code Playgroud)

我想自动化这个过程(在 Dockerfile 中,当提示输入密码时,我将无法手动输入),像这样,

echo 'password' | jupyter notebook password
Run Code Online (Sandbox Code Playgroud)

'password'当提示输入密码验证密码时,这应该自动将 my 输入到 shell

你能给我一个 shell 命令,它可以在没有用户干预的情况下自动设置密码。

linux bash shell ubuntu command-line

5
推荐指数
3
解决办法
6699
查看次数

Java - 字符串不变性和数组可变性

我知道那个数组ab指向与字符串相同的地方s1,s2而不是.为什么?

码:

    String[] a = {"a","b","c"};
    String[] b = a;
    a[0] = "Z";
    String s1 = "hello";
    String s2 = s1;
    s1 = "world";
    System.out.println(Arrays.toString(a) + " - a"); //a and b are same
    System.out.println(Arrays.toString(b) + " - b");
    System.out.println(s1 + " "+ s2); // s1 and s2 are not same.
Run Code Online (Sandbox Code Playgroud)

输出:

    [Z, b, c] - a
    [Z, b, c] - b
    world hello
Run Code Online (Sandbox Code Playgroud)

java string immutability

4
推荐指数
1
解决办法
586
查看次数

不同方法的 OpenCV matchTemplate 阈值

我搜索了很多以找到下面提到的方法的阈值。

methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
            'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', cv2.TM_SQDIFF_NORMED']
Run Code Online (Sandbox Code Playgroud)

我也试图自己弄清楚它们,但我只能找到最大值为 1.0 的 3 种方法的阈值。其他方法的值在 10^5 的范围内。我想知道这些方法的界限。

有人可以指出我正确的方向。我的议程是遍历模板匹配的所有方法并获得最佳结果。我浏览了文档和源代码,但没有运气。

这些是我得到的值,我可以理解 *NORMED 方法的值是 0-1。

cv2.TM_CCOEFF -- 25349100.0
cv2.TM_CCOEFF_NORMED -- 0.31208357214927673
cv2.TM_CCORR -- 616707328.0
cv2.TM_CCORR_NORMED -- 0.9031367897987366
cv2.TM_SQDIFF -- 405656000.0
cv2.TM_SQDIFF_NORMED -- 0.737377941608429
Run Code Online (Sandbox Code Playgroud)

opencv image image-processing template-matching opencv3.0

4
推荐指数
1
解决办法
2742
查看次数

使用队列的树的最大深度

在Narasimha Karumanchi所着的"数据结构和算法"一书中,这是用于找到树的最大深度的代码.

null由于某种原因,他提供了一个队列.我不懂为什么.删除它会破坏代码.

我想知道作者为什么要添加一个null,如果可以用这种方式解决问题,因为我们可以在不添加的情况下解决同样的问题null.

源代码:

public class MaxDepthInBinaryTreeWithLevelOrder {
// Returns the depth of this binary tree. The depth of a binary tree is the
// length of the longest path from this node to a leaf. The depth of a
// binary tree with no descendants (that is, just a leaf) is zero.
public int maxDepthLevelOrder(BinaryTreeNode root){
    if(root == null)
        return 0;
    int maxDepth = 1;
    Queue<BinaryTreeNode> q = new LinkedList<BinaryTreeNode>();
    q.offer(root);
    q.offer(null);   // …
Run Code Online (Sandbox Code Playgroud)

java algorithm tree binary-tree data-structures

2
推荐指数
1
解决办法
559
查看次数