Most readable javascript solution


  • 0
    S
    1. Cover 32 bits of digits.
    2. Reverse built up one array
    3. Join the array and trans back from binary system to decimal.

    Result: 146ms beats 40.58% javascripter.

    var reverseBits = function(n) {
    function createBinaryString (nMask) {
      for (var nFlag = 0, nShifted = nMask, sMask = ""; nFlag < 32;
           nFlag++, sMask += String(nShifted >>> 31), nShifted <<= 1);
      return sMask;
    }
        var r = createBinaryString(n);
        var rt = [];
        for(var i=0; i<32;i++){
        	rt[31-i] = r[i];
        }
    	return parseInt(rt.join(""),2)
    }; 
    

Log in to reply
 

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