关闭

Leetcode-190. Reverse Bits

标签: Leetcode190
100人阅读 评论(0) 收藏 举报
分类:

Reverse bits of a given 32 bits unsigned integer.
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as00111001011110000010100101000000).

Follow up:
If this function is called many times, how would you optimize it?

Related problem: Reverse Integer

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

倒序输出一给定的32位无符号整形数(比如,输入43261596 (二进制 00000010100101000001111010011100);输出(二进制00111001011110000010100101000000));进一步:如果这个函数频繁调用,你改如何优化它?

public static int reverseBits(int n) {

        String a = Integer.toBinaryString(n);

        String b = "";

        for(int i = 0; i < 32 - a.length(); i++){

            b += "0";

        }

        String c = b + a;

        String d =  new StringBuffer(c).reverse().toString();

        String str = "";

        int j = 0;

        for(int i = 0; i < d.length(); i++){

            if(d.substring(i, i+1).equals("1")){

                j = i;

                break;

            }

        }

        str = d.substring(j, d.length());

        int m = 0;

         m = Integer.parseUnsignedInt(str, 2);

        return m;

    } 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15362次
    • 积分:583
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:4篇
    • 译文:33篇
    • 评论:3条
    最新评论