注意:这篇文章上次更新于1602天前,文章内容可能已经过时。
This article was last updated1602 days ago, the content may be outdated.
Help Wanted
Write a function that takes an unsigned integer (in binary string form) and returns the number of ‘1’ bits in its binary representation.
My first version of the code:
1 | class Solution { |
这个我看起来问题不大啊,边界条件设置好,n&1 计算最低位是不是 1 ,再加上递归调用 n 的右移1 位。
This looks fine to me. The base cases are set up, n&1 checks whether the lowest bit is 1, plus the recursive call on n shifted right by 1 bit.
可是一运行却无法通过? 我不明白原因是什么,希望路过的大佬能留言指出。

于是我简单改了一下代码
But it failed to pass when I ran it! I have no idea why, and I hope some expert passing by can leave a comment and point it out.

So I slightly modified the code
1 | class Solution { |
点击提交。

疑惑。
2022 年 3 月 28日
天哪😥,竟然仅仅是因为运算符优先级的问题。
& 运算符的优先级没有 + 运算符高
Click submit.

Confused.
March 28, 2022
Oh my😥, it turned out to be just an operator precedence issue.
The precedence of the & operator is lower than that of the + operator
1 | class Solution { |
还有一个
输入某二叉树的前序遍历和中序遍历的结果,请构建该二叉树并返回其根节点。
假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
例如:

Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
这题本身是一道简单的数据结构问题,按照常规的思路写了代码。
Another One
Construct Binary Tree from Preorder and Inorder Traversal
Given the preorder and inorder traversal results of a binary tree, build the binary tree and return its root node.
Assume that neither the preorder nor the inorder traversal results contain duplicate numbers.
For example:

Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
This problem is a simple data structure exercise, so I wrote the code following the conventional approach.
1 | /** |
上面的代码大概是报了一个类型的错误,我也看不懂什么意思,检查了好多次是不是自己实现的有问题,还是无果。
于是我尝试把先序序列的左右子树序列先切割好,再作为参数传递。
The code above probably reported some type error. I couldn’t understand what it meant, and I checked many times whether my own implementation was wrong, but to no avail.
So I tried slicing the left and right subtree sequences from the preorder sequence first, and then passing them as parameters.
1 | /** |
竟然通过了!
也是很疑惑,C++的语法细节还挺多,这要是 Python ,应该没有这种乱七八糟的问题吧。
后来查了一下问题产生的原因,大概是由于错误的代码中,vector 的构造函数得到的是一个临时变量,应该是即将就销毁的,而我把临时变量以引用的方式传递给函数了,所以出错了。
It passed!
It’s also quite confusing. C++ has so many syntax details. If this were Python, there probably wouldn’t be such messy problems.
Later I looked up the cause of the problem. It’s probably because in the buggy code, the vector constructor produced a temporary variable that was about to be destroyed, and I passed that temporary variable to the function by reference, which caused the error.


