LeetCode: Reverse Bits Solution

String manipulation

Approach

Convert n to binary string

Make the string 32 in length by padding with 0

Reverse the string

Convert back to decimal

Implementation

1var reverseBits = function (n) {
2 return parseInt(
3 n.toString(2).padStart(32, "0").split("").reverse().join(""),
4 2
5 )
6}

References

Original problem

Comments

Loading comments...

Tags

leetcode

string

bit manipulation

Next Post

CSSBattle 15.84: Junction

Grid, transforms, flip

Previous Post

LeetCode: House Robber

Memoized recusion

HoningJS

Search Posts