LeetCode 190 Reverse Bits
来源:互联网 发布:旺旺名字是淘宝昵称么 编辑:程序博客网 时间:2024/06/02 03:54
题目描述
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.
分析
JDK :int reverse(int i)
/** * Returns the value obtained by reversing the order of the bits in the * two's complement binary representation of the specified {@code int} * value. * * @param i the value to be reversed * @return the value obtained by reversing order of the bits in the * specified {@code int} value. * @since 1.5 */ public static int reverse(int i) { // HD, Figure 7-1 i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555; i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333; i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f; i = (i << 24) | ((i & 0xff00) << 8) | ((i >>> 8) & 0xff00) | (i >>> 24); return i; }
思路
把第i位取出,放到结果的第31-i位上。
代码
public static int reverseBits(int n) { int rt = 0; for (int i = 0; i < 32; i++) { rt |= ((n >> i) & 1) << (31 - i); } return rt; }
1 0
- leetCode #190 reverse bits
- LeetCode 190 Reverse Bits
- [LeetCode][190][Reverse Bits]
- LeetCode(190) Reverse Bits
- [LeetCode 190]Reverse Bits
- LeetCode #190 Reverse Bits
- [leetcode 190]Reverse Bits
- LeetCode 190 Reverse Bits
- leetCode 190-Reverse Bits
- leetcode-190 Reverse Bits
- LeetCode 190 Reverse Bits
- leetcode 190 Reverse Bits
- LeetCode 190 :Reverse Bits
- 【leetcode 190 Reverse Bits 】
- leetcode 190:Reverse Bits
- Reverse Bits (leetcode 190)
- leetcode[190]:Reverse Bits
- leetcode-190-Reverse Bits
- 嵌入式tips
- 联想V480”虚拟机被配置为64位客户机操作系统,但是64位操作不可用,已为该虚拟机禁用长模式“的解决办法
- Android Studio - Web View Error - No Such File or directory
- 2015年大二上-数据结构-串(2)- 链串算法库
- project euler 5
- LeetCode 190 Reverse Bits
- online_judge_1018
- 安卓 coordination 等各种特效
- 利用JQuery qrcode来生成二维码
- “update”筛选条件有问题导致的惨案
- hdoj1342Lotto【dfs】
- MHA自动Failover过程解析
- Android中通过浏览器打开一个网页
- hdoj1032The Snail