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. 深入理解python之self

    首先明确的是self只有在类的方法中才会有,独立的函数或方法是不必带有self的.self在定义类的方法时是必须有的,虽然在调用时不必传入相应的参数. self名称不是必须的,在python中self ...

  2. console中一些不常用的实用方法

    console.group('分组1'); console.table( [ {key1: 1,key2: 2,key3: 3}, {key1: 1,key2: 2,key3: 3}, {key1: ...

  3. svg琐碎01

    svg中的<g>主要用来做分组的定位,使用transform="translate(xOffset,yOffset)" 更改起始坐标. transform中的坐标是相对 ...

  4. c#实现串口操作 SerialPort

    命名空间:using System.IO.Ports;该类提供了同步 I/O 和事件驱动的 I/O.对管脚和中断状态的访问以及对串行驱动程序属性的访问. 操作类声明: SerialPort sp = ...

  5. Python中编码问题?

    一.键盘输入 raw_input('请输入:'.decode('utf-8').encode('gbk'))raw_input(unicode('请输入:','utf-8').encode('gbk' ...

  6. Delphi中@,^,#,$特殊符号意义

    概述:   ^: 指针   @: 取址   #: 十进制符   $: 十六进制符   @:取址运算符; var   int:integer;   p:^integer; begin   new(P); ...

  7. [你必须知道的.NET]第三十四回,object成员,不见了!

    发布日期:2009.10.30 作者:Anytao © 2009 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. 在.NET世界了,object是公认的造物主,其麾下的7大成员, ...

  8. tvm install

    一.系统需求:1.可以访问互联网2.关闭防火墙和selinux 二.安装步骤(进入软件包所在目录):1.rpm -ivh daemontools-0.76-1.el6.x86_64.rpm2.yum ...

  9. lintcode:单词切分

    单词切分 给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词. 样例 s = "lintcode" dict = ["lint&qu ...

  10. python_ftplib实现通过FTP下载文件

    1.  Ftplib常用函数介绍 Python中默认安装的ftplib模块定义了FTP类,其中函数有限,可用来实现简单的ftp客户端,用于上传或下载文件,本次主要介绍连接FTP并且进行文件下载功能,可 ...