[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 ...
随机推荐
- LAMP 搭建练习
目录 LAMP 搭建 1:CentOS 7, lamp (module): http + php + phpMyAdmin + wordpress 192.168.1.7 配置虚拟主机 xcache ...
- python3 练习题100例 (十三)
题目十三:将一个正整数分解质因数.例如:输入60,打印出60=2*2*3*5. #!/usr/bin/env python3 # -*- coding: utf-8 -*- ""& ...
- C++ vector 实例
#include <string> #include <iostream> #include <vector> #include <algorithm> ...
- c语言——字符串
char str[] = "http://c.biancheng.net"; char str[] = "http://c.biancheng.net"; ch ...
- Linux命令之---touch
命令简介 linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 命令格式 touch [选项]... 文件... 命令参数 -a 或 ...
- 3 View - Response对象
1. HttpResponse对象:返回数据 from django.http import HttpResponse 在django.http模块中定义了HttpResponse对象的API Htt ...
- service-worker实践
service-worker虽然已列入标准,但是支持的浏览器还是有限制,还有比较多的问题. 1. 生命周期 注册成功-------installing--------------> 安装成功(i ...
- Java并发之(1):volatile关键字(TIJ21-21.3.3 21.3.4)
Java并发Java服务器端编程的一项必备技能. ** 1 简介 volatile是java中的一个保留关键字,它在英语中的含义是易变的,不稳定的.volatile像final.static等其 ...
- leetcode 【 Convert Sorted List to Binary Search Tree 】python 实现
题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...
- git+jenkins持续集成一:git上传代码
先注册一个账号,注册地址:https://github.com/ 记住地址 下载git本地客户端,下载地址:https://git-scm.com/download/win 一路next傻瓜安装,加入 ...