当前位置:网站首页>LeetCode 190. 颠倒二进制位 位运算/easy
LeetCode 190. 颠倒二进制位 位运算/easy
2022-07-27 14:05:00 【押切徹】
1.Description
颠倒给定的 32 位无符号整数的二进制位。
提示:
请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。
在 Java 中,编译器使用二进制补码记法来表示有符号整数。因此,在上面的 示例 2 中,输入表示有符号整数 -3,输出表示有符号整数 -1073741825。
进阶:
如果多次调用这个函数,你将如何优化你的算法?
2.Example
输入:11111111111111111111111111111101
输出:10111111111111111111111111111111
解释:输入的二进制串 11111111111111111111111111111101 表示无符号整数 4294967293,
因此返回 3221225471 其二进制表示形式为 10111111111111111111111111111111 。
3.Solution
1.逐位颠倒
我写的代码使用了数组,其实不用数组就行:
public class Solution {
// you need treat n as an unsigned value
public int reverseBits(int n) {
int[] nums = new int[32];
for(int i=0;i<32;i++) {
if((n&1)==1) {
nums[i] = 1;
}
n = n>>>1;
}
int sum = 0;
for(int i=0;i<32;i++) {
if(nums[i]==1) {
sum += 1<<(31-i);
}
}
return sum;
}
}
官方题解:
public class Solution {
public int reverseBits(int n) {
int res = 0;
for (int i = 0; i < 32; ++i) {
res = (res << 1) | (n & 1);//或|的作用相当于相加
n >>= 1;
}
return res;
}
}
2.分治
有另外一种不使用循环的做法,类似于归并排序。
其思想是分而治之,把数字分为两半,然后交换这两半的顺序;然后把前后两个半段都再分成两半,交换内部顺序……直至最后交换顺序的时候,交换的数字只有 1 位。
以一个 8 位的二进制数字为例:
这里官方题解是从递归的底层开始,递归的第一层交换所有的奇数和偶数位,然后第二层交换两个一组(奇数组和偶数组)的位置。。。。。。
public class Solution {
private static final int M1 = 0x55555555; // 01010101010101010101010101010101
private static final int M2 = 0x33333333; // 00110011001100110011001100110011
private static final int M4 = 0x0f0f0f0f; // 00001111000011110000111100001111
private static final int M8 = 0x00ff00ff; // 00000000111111110000000011111111
public int reverseBits(int n) {
//因为不管什么数和M1进行了一次&操作之后,就只剩下了奇数位的信息
//(因为M1的偶数位都是0,一&的话都变成0了)
//所以或符号|之前的操作的意思是:先将n右移一位,将偶数位信息移到奇数位,再和M1相&之后就得到原来的偶数位
//(现在移到了奇数位,相当于偶数和奇数交换),|之后的意思是将奇数位信息移到偶数位,
//两个操作一|之后就完成了交换。之后操作类似,参考上边的图片。
n = n >>> 1 & M1 | (n & M1) << 1;
n = n >>> 2 & M2 | (n & M2) << 2;
n = n >>> 4 & M4 | (n & M4) << 4;
n = n >>> 8 & M8 | (n & M8) << 8;
return n >>> 16 | n << 16;
}
}
边栏推荐
- Docker practical experience: deploy mysql8 master-slave replication on docker
- 微信小程序实现音乐搜索页面
- Understand the evolution of redis architecture in one article
- 如何帮助企业优化Office管理
- 关于 CMS 垃圾回收器,你真的懂了吗?
- HDU3117 Fibonacci Numbers【数学】
- [popular science] the difference and connection between accuracy and resolution
- Is there a regular and safe account opening platform for gold speculation
- [cache series] completely solve the problems of cache avalanche, breakdown and penetration
- NEFU117 素数个数的位数【素数定理】
猜你喜欢

Research on multi label patent classification based on pre training model

Tencent two sides: @bean and @component are used in the same class, what will happen?

Construction of knowledge map of financial securities and discovery of related stocks from the perspective of knowledge relevance

Disk troubleshooting of kubernetes node

一文搞懂 Redis 架构演化之路

adb命令 (安装apk包格式:adb install 电脑上apk地址包名)

【WORK】关于技术架构

Why is there no unified quotation for third-party testing fees of software products?

Differences among CPU, GPU and NPU

See "sense of security" in uncertainty Volvo asked in 2022
随机推荐
Timestamp of AAC, h264, etc
这年头谁还不会抓包,WireShark 抓包及常用协议分析送给你!
如果我们是那晚负责修复 B 站崩了的开发人员
aac 和 h264等的时间戳
Differences among CPU, GPU and NPU
C language layered understanding (C language array)
Who can't capture packets these days? Wireshark packet capture and common protocol analysis are for you!
视觉系统设计实例(halcon-winform)-10.PLC通讯
The database uses PSQL and JDBC to connect remotely and disconnect automatically from time to time
Unity3D学习笔记10——纹理数组
Lesson 3: SPFA seeking the shortest path
JS epidemic at home, learning can't stop, 7000 word long text to help you thoroughly understand the prototype and prototype chain
【ManageEngine】什么是SIEM
周鸿祎:数字安全能力落后也会挨打
Jmeter录制接口自动化
C语言基础知识梳理总结
MySQL save data prompt: out of range value for column error
Getting started with DirectX
如何做好企业系统漏洞评估
【WORK】关于技术架构