当前位置:网站首页>Leetcode 190. reverse binary bit operation /easy
Leetcode 190. reverse binary bit operation /easy
2022-07-27 15:27:00 【Abcheche】
List of articles
1.Description
Invert given 32 The binary bit of an unsigned integer .
Tips :
Please note that , In some languages ( Such as Java) in , There is no unsigned integer type . under these circumstances , Both input and output will be specified as signed integer types , And should not affect your implementation , Because whether integers are signed or unsigned , Its internal binary representation is the same .
stay Java in , The compiler uses binary complement notation to represent signed integers . therefore , Above Example 2 in , The input represents a signed integer -3, The output represents a signed integer -1073741825.
Advanced :
If you call this function more than once , How will you optimize your algorithm ?
2.Example
Input :11111111111111111111111111111101
Output :10111111111111111111111111111111
explain : Binary string of input 11111111111111111111111111111101 Represents an unsigned integer 4294967293,
Therefore return 3221225471 Its binary representation is 10111111111111111111111111111111 .
3.Solution
1. Bit by bit
The code I wrote uses arrays , In fact, you don't need an array :
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;
}
}
Official explanation :
public class Solution {
public int reverseBits(int n) {
int res = 0;
for (int i = 0; i < 32; ++i) {
res = (res << 1) | (n & 1);// or | The function of is equivalent to adding
n >>= 1;
}
return res;
}
}
2. Divide and conquer
There is another way of not using loops , It's similar to merging and sorting .
Its idea is to divide and rule , Divide the number into two halves , Then exchange the order of the two halves ; Then divide the front and back halves into two halves , Exchange internal order …… Until the last exchange of order , The only numbers exchanged are 1 position .
With a 8 Bit binary digits as an example :
The official solution here starts from the bottom of recursion , The first level of recursion exchanges all odd and even digits , Then the second layer exchanges two groups ( Odd arrays and even groups ) The location of ......
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) {
// Because no matter what number and M1 There was one & After the operation , There is only odd information left
//( because M1 Even digits of are 0, One & All the words become 0 了 )
// So or symbol | The previous operation means : First the n Moves to the right one , Move even digit information to odd digit , And again M1 phase & Then we get the original even digits
//( Now it moves to odd digit , Equivalent to the exchange of even and odd numbers ),| After that, it means to move the odd digit information to the even digit ,
// Two operations one | Then the exchange was completed . After that, the operation is similar , Refer to the picture above .
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;
}
}
边栏推荐
- How to package AssetBundle
- JUC(JMM、Volatile)
- 《剑指Offer》数组中的逆序对
- DevEco Studio2.1运行项目报错
- 3.3-5v conversion
- 网络设备硬核技术内幕 路由器篇 5 汤普金森漫游网络世界(上)
- 魔塔项目中的问题解决
- Selenium reports an error: session not created: this version of chromedriver only supports chrome version 81
- Unity性能优化------渲染优化(GPU)之LOD(Level of detail)
- Simple mathematical knowledge related to 3D
猜你喜欢

STM32 can communication filter setting problem

Four kinds of relay schemes driven by single chip microcomputer

LeetCode 781. 森林中的兔子 哈希表/数学问题 medium

MySQL 面试40连问,面试官你再问下去我可要翻脸了

积分运算电路的设计方法详细介绍
USB interface electromagnetic compatibility (EMC) solution

适配验证新职业来了!华云数据参与国家《信息系统适配验证师国家职业技能标准》编制

Unity performance optimization ----- occlusion culling of rendering optimization (GPU)

See "sense of security" in uncertainty Volvo asked in 2022

修改frameworks资源文件如何单编
随机推荐
Unity最简洁的对象池实现
STM32 can -- can ID filter analysis
《剑指Offer》数组中的逆序对
Unity性能优化------DrawCall
LeetCode 783. 二叉搜索树节点最小距离 树/easy
基于stm32的数字示波器设计方案
LeetCode 90. 子集 II 回溯/medium
MOS管防止电源反接的原理
Selenium 报错:session not created: This version of ChromeDriver only supports Chrome version 81
Reading notes of lifelong growth (I)
4种单片机驱动继电器方案
How to package AssetBundle
网络设备硬核技术内幕 路由器篇 (10) CISCO ASR9900拆解 (四)
谷粒商城配置CorsWebFilter后,报错:Resource sharing error:MultipleAllowOriginValues
LeetCode 74. 搜索二维矩阵 二分/medium
Network equipment hard core technology insider router Chapter 7 tompkinson roaming the network world (Part 2)
Kotlin的基础用法
STM32F10x_硬件I2C读写EEPROM(标准外设库版本)
The first common node of the two linked lists of "Jianzhi offer"
Notice of Shenzhen Municipal Bureau of human resources and social security on the issuance of employment related subsidies for people out of poverty