当前位置:网站首页>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 edit a framework resource file separately
- Stm32f103c8t6 drives sh1106 1.3 "IIC OLED display under Arduino frame
- DIY制作示波器的超详细教程:(一)我不是为了做一个示波器
- USB interface electromagnetic compatibility (EMC) solution
- Deveco studio2.1 operation item error
- 网络设备硬核技术内幕 路由器篇 16 DPDK及其前传(一)
- Principle of MOS tube to prevent reverse connection of power supply
- LeetCode 781. 森林中的兔子 哈希表/数学问题 medium
- generic paradigm
- 网络设备硬核技术内幕 路由器篇 15 从鹿由器到路由器 (下)
猜你喜欢

Several basic uses of tl431-2.5v voltage reference chip

Zhou Hongyi: if the digital security ability is backward, it will also be beaten

LeetCode 面试题 17.21. 直方图的水量 双指针,单调栈/hard

JUC(JMM、Volatile)

What is tor? What is the use of tor browser update?

Dialog manager Chapter 3: create controls

Kubernetes CNI classification / operation mechanism

Selenium 报错:session not created: This version of ChromeDriver only supports Chrome version 81

USB2.0接口的EMC设计方案

工具 - markdown编辑器常用方法
随机推荐
周鸿祎:数字安全能力落后也会挨打
See "sense of security" in uncertainty Volvo asked in 2022
Design scheme of digital oscilloscope based on stm32
网络设备硬核技术内幕 路由器篇 (10) CISCO ASR9900拆解 (四)
TL431-2.5v基准电压芯片几种基本用法
Leetcode-1737-满足三条件之一需改变的最少字符数
西瓜书《机器学习》阅读笔记之第一章绪论
多表查询_子查询概述和多表查询_子查询情况1&情况2&情况3
How "Crazy" is Hefu Laomian, which is eager to be listed, with capital increasing frequently?
MOS管防止电源反接的原理
AssetBundle如何打包
generic paradigm
《终身成长》读书笔记(一)
事务_基本演示和事务_默认自动提交&手动提交
Unity性能优化------DrawCall
同花顺开户在手机开户安全吗?
Four kinds of relay schemes driven by single chip microcomputer
How to package AssetBundle
Sword finger offer merges two sorted linked lists
LeetCode 90. 子集 II 回溯/medium