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位

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

Java: 第 i 次循环移动 i 位

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

Python:

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

Python:

class Solution:
# @param n, an integer
# @return an integer
def reverseBits(self, n):
result = 0
for i in xrange(32):
result <<= 1
result |= n & 1
n >>= 1
return result  

Python:

def reverseBits(n) :
res = 0
while (n > 0) :
res = res << 1
if (n & 1 == 1) :
res = res ^ 1
n = n >> 1 return res

Python:

def reverseBits2(n):
res = 0
while n > 0:
res <<= 1
res |= n & 1
n >>= 1 return res   

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

class Solution(object):
def reverseBits(self, n):
"""
:type n: int
:rtype: int
"""
res = 0
for i in xrange(32):
res <<= 1
res |= ((n >> i) & 1)
return res 

Python: 利用Python的bin()函数

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

C++:

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

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. PHP中的分支及循环语句

    这次实践的都是PHP7的语法. 感觉是以前的5差别不是那么大,只是希望越来越快吧. <?php $looking = isset($_GET['title']) || isset($_GET[' ...

  2. Kotlin属性引用进阶与构造方法引用

    继续还是探讨Kotlin反射相关的知识点,说实话这块不是太好理解,待在实际工作中去对它进行实践慢慢来加深印象. 属性引用进阶: 在Kotlin中的反射其实是跟Java的反射有对应关系的,具体相关的定义 ...

  3. Centos7 yum安装postgresql 9.5

    添加RPM yum install https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7-x86_64/pgdg-centos ...

  4. Json在序列化getter导致的问题

    Java中的Json序列化,不容忽视的getter 问题重现 public class AjaxJson { private boolean success; private String msg; ...

  5. Yarn (转自之乎者也)

    作者:青俞链接:https://www.zhihu.com/question/34016617/answer/57822812来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  6. 学习Spring-Data-Jpa(十七)---对Web模块的支持

    Spring-Data还提供了Web模块的支持,这要求Web组件Spring-MVC的jar包位于classpath下.通常通过使用@EnableSpringDataWebSupport注解来启用继承 ...

  7. LeetCode 947. Most Stones Removed with Same Row or Column

    原题链接在这里:https://leetcode.com/problems/most-stones-removed-with-same-row-or-column/ 题目: On a 2D plane ...

  8. BZOJ 5338: [TJOI2018]xor 可持久化trie+dfs序

    强行把序列问题放树上,好无聊啊~ code: #include <bits/stdc++.h> #define N 200005 #define setIO(s) freopen(s&qu ...

  9. [RN] React Native 实现 类似QQ 登陆页面

    [RN] React Native 实现 类似QQ 登陆页面 一.主页index.js 项目目录下index.js /** * @format */ import {AppRegistry} from ...

  10. 洛谷P2730 [IOI]魔板 Magic Squares

    题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜 ...