【问题标题】:What the difference between my code "++count" and "count++" in the "Kth Smallest Element in a BST" solution?“BST 中的第 K 个最小元素”解决方案中的代码“++count”和“count++”有什么区别?
【发布时间】:2015-07-27 23:12:59
【问题描述】:

在算法问题“Find the Kth Smallest Element in a BST”中 (https://leetcode.com/problems/kth-smallest-element-in-a-bst),我的解决方案如下:

 public class Solution {
        private int count;
        public int kthSmallest(TreeNode root, int k) {
        if (root == null)
            return 0;
        int kth = kthSmallest(root.left, k);
        if (count == k)
            return kth;

        /********** section A *************/
        else if (++count == k)
            return root.val;
        /********** section A end *********/

        /********** section B *************/
        /*
        else if (count == k - 1) 
            return root.val;
        count++;
        */
        /********** section B end *********/

        /********** section C *************/
        /*
        else if (count == k - 1) {
            count++;
            return root.val;
        }
        */
        /********** section C end *********/
        return kthSmallest(root.right, k);
    }
}

leetcode 在线判断,A 段对,B 段错,我认为是相等的,我想不出两个段的区别。

附:添加了C部分。

  • 在A部分

    无论++count == k是真还是假,计数都会增加。

  • 在 B 部分

    计数仅在count == k - 1 为假时增加,因为当count == k - 1 为真时,函数将返回并不再执行。但是既然我们得到了正确的返回值,是不是还要增加count的值呢?

  • 在 C 部分

    计数仅在count == k - 1 时更新

  • 结论 B段不等于A,C段也不等,当我把B段和C段合并时,如下图:

        else if (count == k - 1) {
            count++;
            return root.val;
        }
        count++;
    

相当于A段,通过了线上评委,不如A段优雅。

:)

【问题讨论】:

  • 使用 B 部分时,您的代码不会编译,因为您将在下面有一个悬空的 else 语句。这是你想知道的吗?
  • 如果您将 count++; 行移到 return 语句之前,B 部分将与 A 部分相同。
  • @Codebender 是的,有个小bug,我已经修复了,不影响我的观点。
  • @Pshemo 我已经修复了这个错误,无论“count == k - 1”是真还是假,B 节中的计数都会增加。但是网上评委上的解法还是B节不对。

标签: java algorithm


【解决方案1】:

无论比较是否成功,私有实例变量count的值都会在A节中更新。

也就是说,在部分开始的比较点:

在 A 节中,如果 count == k - 1 成立,则私有 count 变量在返回 root.val 之前增加 1。

在 B 节中,如果 count == k - 1 成立,则不修改 count 变量,并返回 root.val

【讨论】:

  • 如果B节中count == k - 1为真,表示我们得到了返回值,函数结束,为什么还要更新count?
【解决方案2】:

在您的代码中,A 部分中的count 无论如何都会递增,即使它不等于k。 在 B 部分,仅当 count DOES NOT 等于 k-1 时才会增加。

这是因为在使用++i 时,变量会先递增,然后再使用(赋值、比较等)。

【讨论】:

    【解决方案3】:
        int count=3;
       int k=5;
     if (++count == k)
          System.out.println("A"+ count);
    
      else if (count == k - 1) 
           System.out.println("B"+ count);
    
     System.out.println("C"+ count);
    

    我写了类似的代码

    输出; B4 C4

    在第一个 if case 之后,count 仍然是 count =4

    那么 k 将是 4

    如果案例可行,最后第二个

    int count=3;
       int k=5;
     if (count++ == k)
          System.out.println("A"+ count);
    
      else if (count == k - 1) 
           System.out.println("B"+ count);
    
     System.out.println("C"+ count);
    

    输出: B4 C4

    但是如果 int count=4(对于第二个代码块)

    输出: C5

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 2020-03-02
      相关资源
      最近更新 更多