[LeetCode] Number of 1 Bits 位操作
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011
, so the function should return 3.
#include <iostream>
using namespace std;
class Solution {
public:
int hammingWeight(uint32_t n) {
int sum =;
while(n){
if(n&-n) sum++;
n = n&(n-);
}
return sum;
}
}; int main()
{
uint32_t n = ;
Solution sol;
cout<<sol.hammingWeight(n)<<endl;
return ;
}
[LeetCode] Number of 1 Bits 位操作的更多相关文章
- 2016.5.15——leetcode:Number of 1 Bits ,
leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...
- [LeetCode] Number of 1 Bits 位1的个数
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- LeetCode Number of 1 Bits
原题链接在这里:https://leetcode.com/problems/number-of-1-bits/ 题目: Write a function that takes an unsigned ...
- LeetCode——Number of 1 Bits
//求一个整数的二进制串中1的个数 public int hammingWeight(int n) { String b_str = Integer.toBinaryString(n); int b_ ...
- LeetCode Number of 1 Bits 计算1的个数
题意: 提供一个无符号32位整型uint32_t变量n,返回其二进制形式的1的个数. 思路: 考察二进制的特性,设有k个1,则复杂度为O(k).考虑将当前的数n和n-1做按位与,就会将n的最后一个1去 ...
- lc面试准备:Number of 1 Bits
1 题目 Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- [LeetCode] 191. Number of 1 Bits 二进制数1的个数
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- (easy)LeetCode 191.Number of 1 Bits
Number of 1 Bits Write a function that takes an unsigned integer and returns the number of ’1' bits ...
- LeetCode 191. Number of 1 bits (位1的数量)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
随机推荐
- base64转图片上传
不成功,但是有一定的借鉴性 /** * @param base64Codes * 图片的base64编码 */ function sumitImageFile(base64Codes){ consol ...
- Ajax基础知识梳理
Ajax用一句话来说就是无须刷新页面即可从服务器取得数据.注意,虽然Ajax翻译过来叫异步JavaScript与XML,但是获得的数据不一定是XML数据,现在服务器端返回的都是JSON格式的文件. 完 ...
- python3 练习题100例 (十四)
今天逛贴吧,看到有人求助,做了一下.请大家指正! #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'Fan Lijun' imp ...
- 动态拼接SQL语句
1.参考官方文档 ? if:字符判断 ? choose (when, otherwise):分支选择 ? trim (where, set):字符串截取:其中where标签封装查询条件,set标签封装 ...
- Find a path HDU - 5492 (dp)
Find a path Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Uva:11401-Triangle Counting
Triangle Counting Time limit1000 ms Description You are given n rods of length 1, 2-, n. You have to ...
- Poj3061Subsequence
A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 10000, a ...
- Linux 用户行为日志记录
工作中我们常常遇到,有的员工不安于被分配的权限,老是想sudo echo "ziji" /usr/bin/visudo NOPASSWD:ALL来进行提权,造成误删了数据库某条重要 ...
- Mdrill集群安装
Mdrill集群安装 mdrill是阿里妈妈-adhoc-海量数据多维自助即席查询平台下的一个子项目.旨在帮助用户在几秒到几十秒的时间内,分析百亿级别的任意维度组合的数据.mdrill是一个分布式的在 ...
- 如何提高STM32的学习效率
时间如何安排 做任何事情前,习惯写一个计划——要在一个月内上手STM32! 没有计划的日子,每天早上醒来睁开眼睛,却不知道自己今天要干啥 计划和时间安排: 第一阶段:找感觉——谈及STM32,立即反应 ...