Java Solution using Java Functions


  • 4
    S
    public class Solution {
    // you need treat n as an unsigned value
    public int reverseBits(int n) {
        String op = String.format("%32s", Integer.toBinaryString(n)).replace(' ', '0');
        op = new StringBuilder(op).reverse().toString();
        int num = Integer.parseUnsignedInt(op,2);
        return num;
    }}

  • 0
    H

    May I know why you replaced the ' ' with 0?


Log in to reply
 

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