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

分析:题意为反转给定32位无符号整型数的位

思路:我们只需将原整型数从右到左一个一个取出来,然后一个个加到新数的最低位中即可

class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t s=0;
for(int i=0;i<32;i++){
if(n&1==1){
n>>=1;
s=(s<<=1)+1;
}
else{
n>>=1;
s=(s<<=1);
}
}
return s;
}
};

或可参考更简洁做法:

class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t res = 0;
for (int i = 0; i < 32; ++i) {
res |= (((n >> i) & 1) << (31 - i));
}
return res;
}
};

  

其他可参考方法:

#include<iostream>
using namespace std; class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t m=0;
for(int i=0;i<32;i++){
m<<=1;
m = m|(n & 1);
n>>=1;
}
return m;
}
}; int main()
{
uint32_t n = 1;
Solution sol;
cout<<sol.reverseBits(n)<<endl;
return 0;
}

 或:

class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t m = 0;
for (int i = 0; i< 32 ; i++,n/=2)
m = (m<<1) + (n%2);
return m;
}
};

  

 

  

leetcode:Reverse Bits的更多相关文章

  1. LeetCode OJ:Reverse Bits(旋转bit位)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  2. LeetCode 192:Reverse Bits

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  3. LeetCode 190. Reverse Bits (反转位)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  4. [LeetCode] 190. Reverse Bits 颠倒二进制位

    Reverse bits of a given 32 bits unsigned integer. Example 1: Input: 00000010100101000001111010011100 ...

  5. [LeetCode] 190. Reverse Bits 翻转二进制位

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  6. 【leetcode】Reverse Bits(middle)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  7. Java for LeetCode 190 Reverse Bits

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

  8. leetcode:Reverse Integer(一个整数反序输出)

    Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  9. LeetCode 190. Reverse Bits (算32次即可)

    题目: 190. Reverse Bits Reverse bits of a given 32 bits unsigned integer. For example, given input 432 ...

随机推荐

  1. javascript去除首尾空白字符

    if ( twocode.replace(/^\s+|\s+$/g,"")=="" ) { alert("二维码不能为空"); docume ...

  2. linux下安装vsftp

    1. yum安装vsftp # yum install vsftpd 2. 配置Vsftpd 安装完之后我们要对它进行配置,才能正常使用.编辑vsftpd的配置文件vi /etc/vsftpd/vsf ...

  3. ASP.NET页面优化性能提升方法记录

    今天与大家分享:一种优化页面执行速度的方法.采用这个方法,可以使用页面的执行速度获得[8倍]的提升效果. 为了让您对优化的效果有个直观的了解,我准备了下面的测试结果截图: 测试环境:1. Window ...

  4. javascript实现数据结构与算法系列:队列 -- 链队列和循环队列实现及示例

    1 队列的基本概念 队列(Queue):也是运算受限的线性表.是一种先进先出(First In First Out ,简称FIFO)的线性表.只允许在表的一端进行插入,而在另一端进行删除. 队首(fr ...

  5. BZOJ2435: [Noi2011]道路修建

    这种水题真是……没一次AC都不好意思见人啊 P.S. LINUX无限栈真是爽炸了… 我爱递归 /**************************************************** ...

  6. linux 上传/下载文件到windows工具

    一般来说,linux服务器大多是通过ssh客户端来进行远程的登陆和管理的,使用ssh登陆linux主机以后,如何能够快速的和本地机器进行文件的交互呢,也就是上传和下载文件到服务器和本地:   与ssh ...

  7. 压测2.0:云压测 + APM = 端到端压测解决方案

    从压力测试说起 压力测试是确立系统稳定性的一种测试方法,通常在系统正常运作范围之外进行,以考察其功能极限和隐患.与功能测试不同,压测是以软件响应速度为测试目标的,尤其是针对在较短时间内大量并发用户的访 ...

  8. Docker 面临的安全隐患,我们该如何应对

    [编者按]对比虚拟机,Docker 在体量等方面拥有显著的优势.然而,当 DevOps 享受 Docker 带来扩展性.资源利用率和弹性提升的同时,其所面临的安全隐患同样值得重视,近日 Chris T ...

  9. hdu 1538 A Puzzle for Pirates 博弈论

    很经典的问题,思路转载自http://blog.csdn.net/ACM_cxlove?viewmode=contents 题目:这是一个经典问题,有n个海盗,分m块金子,其中他们会按一定的顺序提出自 ...

  10. spring PropertyPlaceholderConfigurer 找不到配置文件原因

    1:  spring 版本问题 参见: http://www.cnblogs.com/alex-blog/archive/2012/12/25/2832357.html 2: bean id 同名   ...