Easy Understanding JavaScript Solution

  • 0

    I found lots of interesting solution. Here is just what I though for a new way to think of that. Please see contents in the code.

     * @param {number} n - a positive integer
     * @return {number} - a positive integer
    var reverseBits = function(n) {
        //first, use >>> to get binary with lead 0 (>>> is just in case, for negative numbers)
        var str = (n >>> 0).toString(2) || '';
        var list = [];
        str ="00000000000000000000000000000000".substr(str.length) + str;
        list = str.split('');
        //reverse the list
        str = list.join('');
        //transfer it to number
        return parseInt(str, 2);

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.