当前位置:网站首页>leetcode_ 191_ 2021-10-15
leetcode_ 191_ 2021-10-15
2022-06-24 21:54:00 【Programming rookie】
leetcode_191_ position 1 The number of
This is a very simple topic , Here I would like to introduce its optimization solution .
Law 1 :
Direct traversal 32 position , Use the bit operation to find the 1 Bit , And then use it oneNums To maintain the .
class Solution {
public:
int hammingWeight(uint32_t n) {
int oneNums = 0;
for(int i = 0; i < 32; ++i){
// Direct traversal n Every one of them
if((n >> i) & 1) // If the bit is 1
oneNums++;
}
return oneNums;
}
};
Law two :
This is the most direct and simple method , Its time complexity is O(N) ( Although it is traversal 32 Time , Can be viewed as O(1), But in order to distinguish from the following methods , It is thought here that O(N)), The space complexity is O(1).
We also have optimization schemes .
Here's a little tip ,Brian Kernighan Algorithm . utilize n & (n - 1) Will n Last digit of 1 become 0. Use this technique , We just need to keep n & n-1, until n be equal to 0. And here n become 0 The number of times experienced is 1 The number of .
class Solution {
public:
int hammingWeight(uint32_t n) {
// Law two : utilize n & (n - 1) Will n Last digit of 1 become 0
int oneNums = 0;
while(n){
n &= n - 1; // Eliminate one bit 0, until n == 0
oneNums++;
}
return oneNums;
}
};
The time complexity is O(K),K yes n In Chinese, it means 1 The number of . The space complexity is O(1).
And we have more advanced methods .
Law three :
This method is similar to dichotomy , Every bit in binary has no other meaning . And what we have to do is to give everyone something else : Each bit in the binary represents 1 A counter , All we have to do is try to add up these counters , That's our answer .
- explain :
consider 3 This unsigned integer ,3 How many binary sequences are there 1 Well ?
Based on this idea , Here's the way .
class Solution {
public:
int hammingWeight(uint32_t n) {
// Law three : Think of each bit as a counter
n = (n & 0x55555555) + ((n >> 1) & 0x55555555);
n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
n = (n & 0xf0f0f0f) + ((n >> 4) & 0xf0f0f0f);
n = (n & 0xff00ff) + ((n >> 8) & 0xff00ff);
n = (n & 0xffff) + ((n >> 16) & 0xffff);
return n;
}
};
- You may not understand what this code is doing , Don't worry. . Let's take a look at the characteristics of these numbers .

n & 0x55555555 Is to count the low order of every two digits 1 The number of .
(n >> 1) & 0x55555555 It is to calculate the high order of every two digits 1 The number of .
Add it up and you get the middle of every two digits 1 The number of .
then n & 0x33333333 Is to count every 4 One of the two lowest places 1 The number of .
(n >> 2) & 0x33333333 Is to calculate every 4 One of the two highest places 1 The number of .
The rest is the same .
The time complexity is O(1), The complexity of space is also O(1).
Law four :( Entertainment Law )
I found this method in a book , The technique used is template meta programming . It just can't be used in this problem , Pure entertainment .
template<int N>
struct OneInBinary {
enum {
value = N % 2 + OneInBinary<N/2>::value };
};
template<>
struct OneInBinary<0> {
enum {
value = 0};
};
// Calling method
OneInBinary<45>::value;
边栏推荐
- 【论】Deep learning in the COVID-19 epidemic: A deep model for urban traffic revitalization index
- Jianmu continuous integration platform v2.5.0 release
- openGauss内核:简单查询的执行
- 【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter
- 机器学习:梯度下降法
- The most important thing at present
- 传输层 udp && tcp
- Intelligent fish tank control system based on STM32 under Internet of things
- 力扣每日一题-第26天-496.下一个更大元素Ⅰ
- 【Camera基础(一)】Camera摄像头工作原理及整机架构
猜你喜欢

力扣每日一题-第26天-496.下一个更大元素Ⅰ
![[product design and R & D collaboration tool] Shanghai daoning provides you with blue lake introduction, download, trial and tutorial](/img/0f/e0b261496d04ca3da8a7d7d19e5bf1.png)
[product design and R & D collaboration tool] Shanghai daoning provides you with blue lake introduction, download, trial and tutorial

AntDB数据库在线培训开课啦!更灵活、更专业、更丰富

leetcode:1504. 统计全 1 子矩形的个数
![在每个树行中找最大值[分层遍历之一的扩展]](/img/5b/81ff20b61c0719ceb6873e44878859.png)
在每个树行中找最大值[分层遍历之一的扩展]

Vscode netless environment rapid migration development environment (VIP collection version)

Graduation design of phase 6 of the construction practice camp

一文理解OpenStack网络

SAP接口debug设置外部断点
![[notes of Wu Enda] convolutional neural network](/img/19/2cac17010c29cbd5ba245de105d6c1.png)
[notes of Wu Enda] convolutional neural network
随机推荐
Datakit 代理实现局域网数据统一汇聚
socket done
架构实战营 第 6 期 毕业总结
[Web Security Basics] some details
火狐拖放后,总会默认打开百度搜索,如果是图片,则会打开图片。
面试官:你说你精通Redis,你看过持久化的配置吗?
煮茶论英雄!福建省发改委、市营商办领导一行莅临育润大健康事业部交流指导
降低pip到指定版本(通过cmp升级pip,在降低到原来版本)
Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)
Several classes of manual transactions
Li Kou daily question - day 26 -496 Next larger element I
即构「畅直播」上线!提供全链路升级的一站式直播服务
Memcached comprehensive analysis – 5 Memcached applications and compatible programs
Why are life science enterprises on the cloud in succession?
Pattern recognition - 1 Bayesian decision theory_ P1
socket(1)
Multi view function in blender
Interpretation of ebpf sockops code
ping: www.baidu. Com: unknown name or service
188. the best time to buy and sell stocks IV