/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ classSolution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> list = newArrayList<Integer>();
/** initialize your data structure here. */ publicMinStack() { stack = newStack<Integer>(); minStack = newStack<Integer>(); }
publicvoidpush(int x) { stack.push(x); if (minStack.isEmpty() || minStack.peek()>=x) minStack.push(x); }
publicvoidpop() { intx= stack.pop(); if (x==minStack.peek()) minStack.pop(); }
publicinttop() { return stack.peek(); }
publicintgetMin() { return minStack.peek(); } }
/** * Your MinStack object will be instantiated and called as such: * MinStack obj = new MinStack(); * obj.push(x); * obj.pop(); * int param_3 = obj.top(); * int param_4 = obj.getMin(); */
/** * Your MinStack object will be instantiated and called as such: * var obj = Object.create(MinStack).createNew() * obj.push(x) * obj.pop() * var param_3 = obj.top() * var param_4 = obj.getMin() */
Submission Detail
18 / 18 test cases passed.
Runtime: 68 ms
Your runtime beats 100.00 % of javascript submissions.
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.
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 2 3 4 5 6 7 8 9 10 11 12
publicclassSolution { // you need treat n as an unsigned value publicintreverseBits(int n) { intres=0; for (inti=31; i >= 0; i--) { if (((n >> i) & 1) == 1) { res += (1 << (31 - i)); } } return res; } }
/** * @param {number} n - a positive integer * @return {number} - a positive integer */ var reverseBits = function(n) { var result = 0; for (var i = 0; i < 32; ++i) { result |= (n >> i & 0x1) << (31 - i); } return result >>> 0; };
Submission Detail
600 / 600 test cases passed.
Runtime: 88 ms
Your runtime beats 12.00 % of javascript submissions.
public static int powerofTwo(int n) {
return 2<<(n-1);
}
5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** * how to multiply two arbitrary integers a and b (a greater than b) * using only bitshifts and addition * @param a * @param b * @return */ publicstaticintmultiply(int a, int b) { intc=0; while (b!=0) { if ((b & 1)!=0) c+=a; a <<= 1; b >>= 1; } return c; }
6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** * how to calculate a sum of two integers a and b * using bitwise operators and zero-testing * @param a * @param b * @return */ publicstaticintsum(int a, int b) { int c=0; while(a!=0) { c = b & a; b = b ^ a; c <<= 1; a = c; } return b; }
JavaScript Bitwise Operators
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