2007年学习数据结构曾经把北大《数据结构与算法》(张铭、赵海燕、王腾蛟)教材书上的c++实现翻译为Java语言,后序周游是最难理解的,现在拿来在LeetCode上跑跑。
Java
1 | /** |
Submission Detail
- 68 / 68 test cases passed.
- Runtime: 4 ms
- Your runtime beats 0.68 % of java submissions.
2007年学习数据结构曾经把北大《数据结构与算法》(张铭、赵海燕、王腾蛟)教材书上的c++实现翻译为Java语言,后序周游是最难理解的,现在拿来在LeetCode上跑跑。
1 | /** |
2007年学习数据结构曾经把北大《数据结构与算法》(张铭、赵海燕、王腾蛟)教材书上的c++实现翻译为Java语言,现在拿来在LeetCode上跑跑,不是很理解。
1 | /** |
Although its performance is not good, solution of Binary Tree Level Order Traversal is clear.
1 | /** |
2007年学习数据结构曾经用Java实现过,虽然还是知其然而不知其所以然,北大那本《数据结构与算法》(张铭、赵海燕、王腾蛟)教材上是用C++实现的。
1 | /** |
LeetCode – Divide Two Integers (Java) is the right answer, tested.
This problem can be solved based on the fact that any number can be converted to the format of the following, Power series of 2:
num=a_0*2^0+a_1*2^1+a_2*2^2+...+a_n*2^n
1 | class Solution { |
The solution of two stack is clear for me.
1 | class MinStack { |
1 | /** |
Simple as it labled, The problem is puzzled for it has problem with unsigned integer, and neith Java nor JavaScript has unsigned type. I’d like to pass it for some time. The code copied from Web passed in LeetCode, but it output wrong result for some input number.
solution from 190. Reverse Bits
There is no unsigned integer type. If you need to work with unsigned values originating outside your program, they must be stored in a larger signed type. For example, unsigned bytes produced by an analog-to-digital converter, can be read into variables of type short. — THE Java™ Programming Language, Fourth Edition, By Ken Arnold, James Gosling, David Holmes
1 | public class Solution { |
solution from 190. Reverse Bits – JavaScript 代码
1 | /** |
Operator | Description |
---|---|
| | Bitwise OR |
& | Bitwise AND |
~ | Bitwise Complement |
^ | Bitwise XOR |
<< | Left Shift |
>> | Right Shift |
>>> | Unsigned Right Shift |
System.out.printf("%d*%d=%d", 2,8,2<<3);
System.out.printf("%d/2=%d",11,11>>1);
public static boolean isOddNum(int n) {
return (n & 1)==1;
}
public static int powerofTwo(int n) {
return 2<<(n-1);
}
1 | /** |
1 | /** |
Operator | Name | Description |
---|---|---|
& | AND | Sets each bit to 1 if both bits are 1 |
| | OR | Sets each bit to 1 if one of two bits is 1 |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left | shift Shifts left by pushing zeros in from the right and let the leftmost bits fall off |
>> | Signed right shift | Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
>>> | Zero fill right | shift Shifts right by pushing zeros in from the left, and let the rightmost bits fall off |
A linked list can be reversed either iteratively or recursively. Could you implement both?
© LeetCode – Reverse Linked List (Java) - Java Solution 1 - Iterative
1 | /** |
1 | /** |
© LeetCode – Reverse Linked List (Java) - Java Solution 2 - Recursive
1 | /** |
1 | /** |
1 | /** |
虽然是Easy的题,自己却想不出来,最后只好参考了这个 Given a string, find its first non-repeating character 的思路,倒也很容易看明白,一点就破的题。
1 | class Solution { |
1 | /** |