191 Number of 1 Bits 位1的个数
编写一个函数,输入是一个无符号整数,返回的是它所有 位1 的个数(也被称为汉明重量)。
例如,32位整数 '11' 的二进制表示为 00000000000000000000000000001011,所以函数返回3。
详见:https://leetcode.com/problems/number-of-1-bits/description/
Java实现:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int cnt=0;
while(n!=0){
n=(n-1)&n;
++cnt;
}
return cnt;
}
}
C++实现:
方法一:
class Solution {
public:
int hammingWeight(uint32_t n) {
int cnt=0;
unsigned int flag=1;
while(flag)
{
if(flag&n)
{
++cnt;
}
flag<<=1;
}
return cnt;
}
};
方法二:
class Solution {
public:
int hammingWeight(uint32_t n) {
int cnt=0;
while(n)
{
n=(n-1)&n;
++cnt;
}
return cnt;
}
};
191 Number of 1 Bits 位1的个数的更多相关文章
- [LeetCode] 191. Number of 1 Bits ☆(位 1 的个数)
描述 Write a function that takes an unsigned integer and return the number of '1' bits it has (also kn ...
- leetcode 191 Number of 1 Bits(位运算)
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- [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] 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 ...
- Leetcode#191. Number of 1 Bits(位1的个数)
题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...
- LN : leetcode 191 Number of 1 Bits
lc 191 Number of 1 Bits 191 Number of 1 Bits Write a function that takes an unsigned integer and ret ...
- 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 ...
- LeetCode 191 Number of 1 Bits
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
- 191. Number of 1 Bits
题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
随机推荐
- SQL 用于各种数据库的数据类型
SQL 用于各种数据库的数据类型 Microsoft Access.MySQL 和 SQL Server 所使用的数据类型和范围. Microsoft Access 数据类型 数据类型 描述 存储 T ...
- CF 234 C Weather(粗暴方法)
C. Color Stripe time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- 【JS】JavaScript引擎的内部执行机制
近期在复习JavaScript,看到setTimeout函数时.想起曾经刚学时,在一本书上看过setTimeout()里的回调函数执行的间隔时间有昌不是后面设置的值.曾经没想太多.网上看了JS大 ...
- linux安装anaconda中的问题及解决办法
安装过程: 0:在ananconda官网网站上下载anaconda的linux版本https://www.anaconda.com/download/: 1:linux上切换到下载目录后(用cd), ...
- 配置server禁止全部非法域名 訪问自己的server
1.Apache2.4.1曾经: 第一种 直接拒绝訪问 打开 httpd.conf 文件,将一下配置追加到文件最后. #直接拒绝全部非法域名 <VirtualHost *:80> Ser ...
- 关于集成支付宝SDK的开发
下载 首先,你要想找到这个SDK,都得费点功夫.如今的SDK改名叫移动支付集成开发包了,下载页面在 这里 的 "请点此下载集成开发包" Baidu和Googlep排在前面的支付宝开 ...
- RabbitMQ常用命令、管理界面
1.运行CMD,cd切换到RabbitMQ安装目录sbin下E:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.2\sbin 执行 rabbitm ...
- iOS开发——高级篇——iOS涂鸦画板效果实现
一个简单的绘图应用,模仿苹果自带软件备忘录里的涂鸦功能 核心代码 #import "DrawView.h" #import "DrawPath.h" @inte ...
- idea自定义文档注释模板
1.类注释模板 IntelliJ IDE --> Preferences --> Editor --> File and Code Templates --> Includes ...
- HDU3001 Travelling —— 状压DP(三进制)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/ ...