当前位置:网站首页>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;
边栏推荐
- Visit Amazon memorydb and build your own redis memory database
- Multi view function in blender
- Excel布局
- Object.defineProperty和Reflect.defineProperty的容错问题
- 字节的软件测试盆友们你们可以跳槽了,这还是你们心心念念的字节吗?
- Datakit agent realizes unified data aggregation in LAN
- Multi task model of recommended model: esmm, MMOE
- 我国SaaS产业的发展趋势与路径
- 【无标题】
- Li Kou daily question - day 25 -496 Next larger element I
猜你喜欢

Installing Oracle without graphical interface in virtual machine centos7 (nanny level installation)

Volcano成Spark默认batch调度器

心楼:华为运动健康的七年筑造之旅

多路转接select

介绍BootLoader、PM、kernel和系统开机的总体流程

使用region折叠代码

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

Why are life science enterprises on the cloud in succession?

leetcode-201_2021_10_17

Multiplexer select
随机推荐
为什么生命科学企业都在陆续上云?
How to achieve energy conservation and environmental protection of full-color outdoor LED display
Advanced secret of xtransfer technology newcomers: the treasure you can't miss mentor
how to install clustershell
使用Adb连接设备时提示设备无权限
LeetCode-513. Find the value in the lower left corner of the tree
Suspend component and asynchronous component
队列实现原理和应用
EasyBypass
Intelligent fish tank control system based on STM32 under Internet of things
《各行业零代码企业应用案例集锦》正式发布
降低pip到指定版本(通过PyCharm升级pip,在降低到原来版本)
【Camera基础(二)】摄像头驱动原理和开发&&V4L2子系统驱动架构
使用region折叠代码
Several classes of manual transactions
Fuzhou business office of Fujian development and Reform Commission visited the health department of Yurun university to guide and inspect the work
[notes of Wu Enda] multivariable linear regression
【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter
188. the best time to buy and sell stocks IV
(待补充)GAMES101作业7提高-实现微表面模型你需要了解的知识