当前位置:网站首页>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;
}
};
位运算常用技巧

边栏推荐
- Why neural networks are ineffective?
- Pointnet++ Chinese Translation
- [embedded C foundation] Part 6: super detailed explanation of common input and output functions
- Bull spread portfolio
- Chapter 6 提升
- Redefinition problem of defining int i variable in C for loop
- 2020jenkins study notes
- Fundamentals of machine learning Bayesian analysis-14
- [embedded explanation] key scanning based on finite state machine and stm32
- Protective bearish strategy
猜你喜欢

How to improve deep learning performance?

PCP parity principle arbitrage

Fundamentals of machine learning Bayesian analysis-14
![[July 5 event preview] Flink Summit](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[July 5 event preview] Flink Summit

【嵌入式C基础】第4篇:运算符的使用

管理区解耦架构见过吗?能帮客户搞定大难题的

Summary: idea problem record

Analysis of Andriod low on memory printing principle
![[June 28 event preview] low code Summit](/img/ba/2f35306ed2fb0ece14d704256d8e60.png)
[June 28 event preview] low code Summit

Definition of option basis
随机推荐
Installation and reinstallation of win11 system graphic version tutorial
[embedded C foundation] Part 5: original code / inverse code / complement code
Fundamentals of machine learning - principal component analysis pca-16
SSM框架网上书城全套
Mysql中DQL基本练习
gicv3 spi register
[graduation design] oscilloscope design and Implementation Based on STM32 - single chip microcomputer Internet of things
【嵌入式C基础】第4篇:运算符的使用
10、 Kubernetes scheduling principle
Fundamentals of machine learning Bayesian analysis-14
黑猫带你学eMMC协议第27篇:什么是eMMC的动态容量(Dynamic Capacity)?
2020-12-07
Full disclosure! Huawei cloud distributed cloud native technology and Practice
butterfly spreads
Summary: idea problem record
[Bi design teaching] STM32 and FreeRTOS realize low power consumption
[FPGA]: Joint Simulation of FPGA and MATLAB
Led aquarium lamp touch chip-dlt8t02s-jericho
The form select in antd is received before it is selected
Analysis of Andriod low on memory printing principle