当前位置:网站首页>leetcode-201_ 2021_ 10_ seventeen
leetcode-201_ 2021_ 10_ seventeen
2022-06-24 21:53:00 【Programming rookie】
leetcode-201_ Invert binary bit
Law 1 :
Classic practice , It's also the simplest way . Create a new object ret To maintain the reversed results . Traverse n Of 32 position , If the first i Position as 1, that ret Of the (31-i) Position as 1. One small detail is if n The highest of 1 Has been traversed , Then you can finish ahead of time , Because the back is full of 0.
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t ret = 0;
for(int i = 0; i < 32; ++i){
if(!(n >> i)) // Optimize , If n >> i by 0, Then there is no need to recycle
break;
if((n >> i) & 1){
ret |= (1 << (31 - i));
}
}
return ret;
}
};
Law two :
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
n = ((n >> 16) & m1) + ((n << 16) & M1);
n = ((n >> 8) & m2) + ((n << 8) & M2);
n = ((n >> 4) & m3) + ((n << 4) & M3);
n = ((n >> 2) & m4) + ((n << 2) & M4);
n = ((n >> 1) & m5) + ((n << 1) & M5);
return n;
}
private:
int m1 = 0xffff; // 0000 0000 0000 0000 1111 1111 1111 1111
int M1 = 0xffff0000;
int m2 = 0xff00ff;
int M2 = 0xff00ff00;
int m3 = 0xf0f0f0f;
int M3 = 0xf0f0f0f0;
int m4 = 0x33333333;
int M4 = 0xcccccccc;
int m5 = 0x55555555;
int M5 = 0xaaaaaaaa;
};
Method 2 is a very clever algorithm . Invert a number , It's going to be the front 16 After bit becomes 16 position , after 16 Bit change to front 16 position . And then in each 16 In a , Before the 8 After bit becomes 8 position , after 8 Bit becomes Front 8 position . Then at every 8 Half and half of the bits are swapped again … Until the swapped bits become 1. We can easily implement this process by using masks .

The process of exchange requires a mask to implement , We will n >> 16 Before it needs to be cleared 16 The influence of position , So we need to & On
0000 0000 0000 0000 1111 1111 1111 1111,n << 16 Orthomorphism , Then add the two together to exchange the former 16 Position and back 16 position .
take n >> 8 Behind you , In order to eliminate the influence of unnecessary bits , need & On 0000 0000 1111 1111 0000 0000 1111 1111, Keep the second 1-8 Bit and 17-24 Digit number , The other bits are cleared . Moving left is the same . Then add the two together and swap the former 8 Position and back 8 position .
The following are the same .
边栏推荐
- Application practice | massive data, second level analysis! Flink+doris build a real-time data warehouse scheme
- 架构实战营 第 6 期 毕业总结
- [camera Foundation (I)] working principle and overall structure of camera
- [camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture
- Unity about conversion between local and world coordinates
- 机器学习:梯度下降法
- [product design and R & D collaboration tool] Shanghai daoning provides you with blue lake introduction, download, trial and tutorial
- A deep learning model for urban traffic flow prediction with traffic events mined from twitter
- VSCode无网环境快速迁移开发环境(VIP典藏版)
- Li Kou daily question - day 25 -496 Next larger element I
猜你喜欢

Multi view function in blender

如何做到全彩户外LED显示屏节能环保

2022 international women engineers' Day: Dyson design award shows women's design strength

2022国际女性工程师日:戴森设计大奖彰显女性设计实力
![[notes of Wu Enda] convolutional neural network](/img/19/2cac17010c29cbd5ba245de105d6c1.png)
[notes of Wu Enda] convolutional neural network

Multi task model of recommended model: esmm, MMOE
![[精选] 多账号统一登录,你如何设计?](/img/df/9b4fc11a6971ebe8162ae84250a782.png)
[精选] 多账号统一登录,你如何设计?

【吴恩达笔记】多变量线性回归

Graduation design of phase 6 of the construction practice camp
![[notes of wuenda] fundamentals of machine learning](/img/71/6192a75446fa7f79469a5483ececc6.jpg)
[notes of wuenda] fundamentals of machine learning
随机推荐
架构实战营 第 6 期 毕业总结
Memcached full profiling – 1 Fundamentals of memcached
Ebpf XDP mount point analysis
leetcode_1470_2021.10.12
直击“三夏”生产:丰收喜报频传 夏播紧锣密鼓
数据链路层 && 一些其他的协议or技术
Structured interview of state-owned enterprises and central enterprises employment of state-owned enterprises Modou Interactive Employment Service steward
Why are life science enterprises on the cloud in succession?
建木持续集成平台v2.5.0发布
TDengine可通过数据同步工具 DataX读写
Network layer & IP
Pattern recognition - 1 Bayesian decision theory_ P1
[featured] how do you design unified login with multiple accounts?
基于 KubeSphere 的分级管理实践
PKI notes
【Camera基础(一)】Camera摄像头工作原理及整机架构
基于ASP.NET开发的固定资产管理系统源码 企业固定资产管理系统源码
为什么生命科学企业都在陆续上云?
03---增反膜
[精选] 多账号统一登录,你如何设计?