码迷,mamicode.com
首页 > 其他好文 > 详细

leetCode 190-Reverse Bits

时间:2015-04-01 20:01:40      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:leetcode   190   reverse bits   

链接: https://leetcode.com/problems/reverse-bits/

此题的关键是预先将1<<i的数字存入一个大小为32的数组中,然后通过x & (1 << i)来获得x的第i位是不为0的判断.进行求和即可。

class Solution {
public:
    Solution(){
        unsigned int i = 0;
		unsigned int j = 1;
		for(; i < 32; i++)
			a[i] = (j<<(31-i));
	}
	unsigned int reverseBits(unsigned int n) {
        unsigned int reverseBit = 0;
        for(unsigned int i = 0; i < 32; i++){
            if((n & (1 << i)) != 0) reverseBit += a[i];
        }
        return reverseBit;
    }
private:
    unsigned int a[32]; 
};


leetCode 190-Reverse Bits

标签:leetcode   190   reverse bits   

原文地址:http://blog.csdn.net/lu597203933/article/details/44811463

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!