2ms Java Solution


  • 12
    W
    public class Solution {
        // you need treat n as an unsigned value
        public int reverseBits(int n) {
            int answer = 0; // initializing answer
            for (int i = 0; i < 32; i++) { // 32 bit integers
                answer <<= 1; // shifts answer over 1 to open a space
                answer |= ((n >> i) & 1); // inserts bits from n
            }
            return answer;
        }
    }
    

    If anyone has a faster solution, please comment below.


  • 0

    yours is 3 ms.


Log in to reply
 

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