当前位置:网站首页>leetcode-190.颠倒二进制位
leetcode-190.颠倒二进制位
2022-07-28 12:08:00 【KGundam】
位运算
题目详情
颠倒给定的 32 位无符号整数的二进制位。
提示:
请注意,在某些语言(如 Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。
在 Java 中,编译器使用二进制补码记法来表示有符号整数。因此,在 示例 2 中,输入表示有符号整数 -3,输出表示有符号整数 -1073741825。
示例1:
输入:n = 00000010100101000001111010011100
输出:964176192 (00111001011110000010100101000000)
解释:输入的二进制串 00000010100101000001111010011100 表示无符号整数 43261596,
因此返回 964176192,其二进制表示形式为 00111001011110000010100101000000。
示例2:
输入:n = 11111111111111111111111111111101
输出:3221225471 (10111111111111111111111111111111)
解释:输入的二进制串 11111111111111111111111111111101 表示无符号整数 4294967293,
因此返回 3221225471 其二进制表示形式为 10111111111111111111111111111111 。
思路:
利用算术左移和右移,即可实现翻转,每次res左移在右侧补0,然后取n最后一位填入res
然后令n右移,以便取下一位,举个例子:
我的代码:
class Solution
{
public:
uint32_t reverseBits(uint32_t n)
{
uint32_t res = 0;
for (int i = 0; i < 32; ++i)
{
res <<= 1; //答案左移
res += n & 1; //取n末位
n >>= 1; //n右移
}
return res;
}
};
还可以利用分治法,把二进制串分成两部分,交换左右两部分,然后再用同样的方法分别处理左右两部分直到将所有部分都完成交换,即完成了翻转
class Solution
{
public:
uint32_t reverseBits(uint32_t n)
{
n = (n >> 16) | (n << 16);
n = ((n & 0xff00ff00) >> 8) | ((n & 0x00ff00ff) << 8);
n = ((n & 0xf0f0f0f0) >> 4) | ((n & 0x0f0f0f0f) << 4);
n = ((n & 0xcccccccc) >> 2) | ((n & 0x33333333) << 2);
n = ((n & 0xaaaaaaaa) >> 1) | ((n & 0x55555555) << 1);
return n;
}
};
位运算常用技巧

边栏推荐
- How to open the power saving mode of win11 system computer
- Installation and reinstallation of win11 system graphic version tutorial
- [July 5 event preview] Flink Summit
- SSM框架网上书城全套
- Remove the plug-in of category in WordPress link
- [embedded C foundation] Part 8: explanation of C language array
- What if the right button of win11 start menu doesn't respond
- Interview must ask, focus! Tell me about the Android application startup process and its source code?
- Form for real-time custom verification
- How many times can the WordPress user name be changed? Attach the method of changing user name
猜你喜欢

Form for real-time custom verification
![[graduation design] oscilloscope design and Implementation Based on STM32 - single chip microcomputer Internet of things](/img/c7/87027ded6d9e522952827fae0fccbd.png)
[graduation design] oscilloscope design and Implementation Based on STM32 - single chip microcomputer Internet of things
![[pictures and texts] detailed tutorial of one click reinstallation of win11 system](/img/cc/749fe4095fc5afb1fc2c65df43d06c.png)
[pictures and texts] detailed tutorial of one click reinstallation of win11 system

《TiDB 6.x in Action》发布,凝聚社区集体智慧的 6.x 实践汇总!

SSM框架网上书城全套

企业数字化本质

Summary: idea problem record

UV germicidal lamp chip dlt8p65sa Jericho

【嵌入式C基础】第6篇:超详细的常用的输入输出函数讲解
![[FPGA] FIR filter - half band filter](/img/6e/d97b3842f80e37aa41b888384a14cb.png)
[FPGA] FIR filter - half band filter
随机推荐
Pointnet++ Chinese Translation
Extended operator
What if the win11 folder cannot be opened
Protobuf data exchange format
机器学习基础-决策树-12
.net for subtraction, intersection and union of complex type sets
Analysis of Andriod low on memory printing principle
Summary: idea problem record
Vditor 渲染器如何做到服务端渲染(SSR)?
[graduation design] smart home system based on ZigBee - single chip microcomputer Internet of things stm32
[embedded explanation] key scanning based on finite state machine and stm32
Is jetpack compose completely out of view?
【嵌入式C基础】第5篇:原码/反码/补码
[embedded C foundation] Part 5: original code / inverse code / complement code
Black cat takes you to learn EMMC protocol chapter 27: what is EMMC's dynamic capacity?
Getderivedstatefromprops lifecycle
2020-12-07
[FPGA]: MATLAB generates COE files
Fundamentals of machine learning Bayesian analysis-14
[graduation design teaching] ultrasonic ranging system based on single chip microcomputer - Internet of things embedded stm32