Reverse bits of a given 32 bits unsigned integer.

For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).

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.

将输入转换成2进制字符串,再翻转并扩充到32位,再将此32位的二进制转为无符号整数

int 在内存中以二进制形式存储,占据32位。用一个 int 型整数 ans 来记录结果,采用移位操作,因为:1. 注意到移位操作比乘2、除2操作效率更高,2. 移位操作很好地绕开了整型运算的溢出以及符号问题。在每次循环中:ans 每次左移一位,当 n 的最低位为1时,ans 的最低位就变为1,n 每次右移一位。总共进行32次循环。

Java: 每次只需移动1位

  1. class Solution {
  2. public int reverseBits(int n) {
  3. int ans = 0;
  4. for (int i = 0; i < 32; i++) {
  5. ans <<= 1;
  6. if ((n & 1) == 1)
  7. ans++;
  8. n >>= 1;
  9. }
  10. return ans;
  11. }
  12. }

Java: 第 i 次循环移动 i 位

  1. class Solution {
  2. public int reverseBits(int n) {
  3. int ans = 0;
  4. for (int i = 0; i < 32; i++)
  5. ans |= ((n >> i) & 1) << (31 - i);
  6. return ans;
  7. }
  8. }

Python:

  1. class Solution:
  2. # @param n, an integer
  3. # @return an integer
  4. def reverseBits(self, n):
  5. string = bin(n)
  6. if '-' in string:
  7. string = string[:3] + string[3:].zfill(32)[::-1]
  8. else:
  9. string = string[:2] + string[2:].zfill(32)[::-1]
  10. return int(string, 2)

Python:

  1. class Solution:
  2. # @param n, an integer
  3. # @return an integer
  4. def reverseBits(self, n):
  5. result = 0
  6. for i in xrange(32):
  7. result <<= 1
  8. result |= n & 1
  9. n >>= 1
  10. return result  

Python:

  1. def reverseBits(n) :
  2. res = 0
  3. while (n > 0) :
  4. res = res << 1
  5. if (n & 1 == 1) :
  6. res = res ^ 1
  7. n = n >> 1
  8.  
  9. return res

Python:

  1. def reverseBits2(n):
  2. res = 0
  3. while n > 0:
  4. res <<= 1
  5. res |= n & 1
  6. n >>= 1
  7.  
  8. return res   

Python: 将n的二进制表示从低位到高位的值依次取出,逆序排列得到翻转后的值。

  1. class Solution(object):
  2. def reverseBits(self, n):
  3. """
  4. :type n: int
  5. :rtype: int
  6. """
  7. res = 0
  8. for i in xrange(32):
  9. res <<= 1
  10. res |= ((n >> i) & 1)
  11. return res 

Python: 利用Python的bin()函数

  1. class Solution(object):
  2. def reverseBits(self, n):
  3. """
  4. :type n: int
  5. :rtype: int
  6. """
  7. b = bin(n)[:1:-1]
  8. return int(b + '0'*(32-len(b)), 2)  

C++:

  1. class Solution {
  2. public:
  3. uint32_t reverseBits(uint32_t n) {
  4. uint32_t res = 0;
  5. for (int i = 0; i < 32; ++i) {
  6. if (n & 1 == 1) {
  7. res = (res << 1) + 1;
  8. } else {
  9. res = res << 1;
  10. }
  11. n = n >> 1;
  12. }
  13. return res;
  14. }
  15. };  

LeetCode中有关位操作的题:

[LeetCode] 191. Number of 1 Bits 二进制数1的个数

Repeated DNA Sequences 求重复的DNA序列

Single Number 单独的数字

Single Number II 单独的数字之二

Grey Code 格雷码

类似题目:

[LeetCode] 191. Number of 1 Bits 二进制数1的个数

[LeetCode] 7. Reverse Integer 翻转整数

All LeetCode Questions List 题目汇总

[LeetCode] 190. Reverse Bits 翻转二进制位的更多相关文章

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

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

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

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

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

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

  4. Java for LeetCode 190 Reverse Bits

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

  5. Leetcode 190. Reverse Bits(反转比特数)

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

  6. 190 Reverse Bits 颠倒二进制位

    颠倒给定的32位无符号整数的二进制位.例如,给定输入 43261596(二进制表示为 00000010100101000001111010011100 ),返回 964176192(二进制表示为 00 ...

  7. Java [Leetcode 190]Reverse Bits

    题目描述: everse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represente ...

  8. Leetcode 190 Reverse Bits 位运算

    反转二进制 class Solution { public: uint32_t reverseBits(uint32_t n) { uint32_t ans = ; ; i<; ++i,n &g ...

  9. 【LeetCode】190. Reverse Bits 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二进制字符串翻转 位运算 日期 题目地址:https://le ...

随机推荐

  1. jmeter+nmon+crontab简单的执行接口定时压测

    一.概述 临时接到任务要对系统的接口进行压测,上面的要求就是:压测,并发2000 在不熟悉系统的情况下,按目前的需求,需要做的步骤: 需要有接口脚本 需要能监控系统性能 需要能定时执行脚本 二.观察 ...

  2. wpscan 更新超时报错

    wpscan更新超时报错 本人亲测方法2 https://data.wpscan.org/plugins.json https://data.wpscan.org/plugins.json.sha51 ...

  3. Java调用Kotlin事项及Kotlin反射初步

    继续来研究Java调用Kotlin的一些东东. @Throws注解: 我们知道在Kotlin中是不存在checked exception的,而在Java中是存在的,那..如果从Java来调用Kotli ...

  4. linux用户的问题

    最近在开发的时候遇到一个问题: 我在某个项目下的某个文件夹内写了一个可以单独run的A.py文件,这个文件里面的代码可以调用kubernetes的python接口来请求kubernetes上的信息(比 ...

  5. Sql 数据库 用户密码MD5加密

    直接给代码先 DECLARE @TAB TABLE( NAEM VARCHAR(50) ) DECLARE @PA VARCHAR(50) DECLARE @A VARCHAR(10) SET @A= ...

  6. 持续集成学习1 gitlab和jenkins安装

    一.gitlab安装参照链接 https://www.cnblogs.com/linuxk/p/10100431.html 二.安装jenkins 1.获取jenkins源码包 https://blo ...

  7. linux命令之------Tar解压缩

    Tar解压缩 作用:将解压缩后缀名为tar的压缩包 -f<备份文件>或—file=<备份文件>指定备份文件 -v或-verbose显示指令执行过程 -x或-extract或-g ...

  8. javascript 中的方法注入

    js 中的方法注入 java中很多框架支持 apo 的注入, js中也可以类似的进行实现 主要是通过扩展js中方法的老祖 Function 对象来进行实现. Function.prototype.af ...

  9. Go-Json操作

    /** * @Author: jadeshu * @Description: * @File: main * @Version: 1.0.0 * @Date: 2019/11/7 2:33 */ pa ...

  10. 【0521模拟赛】小Z爱划水

    题目描述 小Z和其它机房同学都面临一个艰难的抉择,那就是 要不要划水? 每个人都有自己的一个意见,有的人想做题,有的人想划水. 当然,每个人只能选择一个事情做.如果一个人做的事情和他想做的不同,那么他 ...