Reverse Bits
来源:互联网 发布:linux 关闭ntpdate 编辑:程序博客网 时间:2024/05/29 09:24
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?
JAVA解法:
解法1:32位的整数,从最低位与最高位开始分别观察,若对应位置上异或为1,则需交换两者的位置。否则不交换;
解法2: 32位的整数,从最低位开始遍历,每位向左移位(32-i-1)位。
public class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { int result = 0; if((n == 0) || (n == 0xffffffff )) { return n; } for(int k = 0; k < 32; k++) { if((n & 0x01) == 1) { result |= (1 << (32-k-1)); } n = n>>1; } return result; }}
int Int_Size = Integer.SIZE; if((n == 0) || (n == 0xffffffff )) { return n; } for(int i = 0; i < Int_Size/2; i++) { int j = Int_Size-1-i; int low = (n >> i) & 1; int high = (n >> j) & 1; int a = 1 << i; int b = 1 << j; if((low ^ high) == 1) { n = n ^ (a|b); } } return n; }}
0 0
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Reverse Bits
- Google App Engine的app.yaml详细说明
- 刚刚提取了金山毒霸的[垃圾清理] 和 [隐私清理], 分享方法,大家可以自己试一下
- emmc跟踪代码中的:might_sleep
- Java中的锁
- 7.12 noip2103提高组复赛day2
- Reverse Bits
- 【bzoj4552】【Tjoi2016&Heoi2016】【NOIP2016模拟7.12】排序
- C语言入门(十八)数组和指针
- LeetCode 350. Intersection of Two Arrays II
- 网络爬虫(一):抓取网页的含义和URL基本构成
- android studio加载程序太慢了,给你解决方案
- SVN switch 用法详解
- ReactNative学习资源整合
- NKOI 1316 小白逛公园