LeetCode 191 Number of 1 Bits
Problem:
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.
Summary:
求三十二位无符号数含1的位数。
Analysis:
1. 最简单的思路为将n移位直至n为0,每移位一次判断最低位是否为1。但若有1在最高位则需移位32次,效率太低。
class Solution {
public:
int hammingWeight(uint32_t n) {
int cnt = ;
while (n) {
cnt += n & ;
n = n >> ;
} return cnt;
}
};
2. 下面这种思路能够把移位的次数降低至与含1的位数相等,大大提高了效率。
举个栗子:若n = 1010010,则:
- n = 1010010 n - 1 = 1010001 n & (n - 1) = 1010000
- n = 1010000 n - 1 = 1001111 n & (n - 1) = 1000000
- n = 1000000 n - 1 = 0111111 n & (n - 1) = 0000000
可以看出,每进行一次n & (n - 1)操作,最低位上的1就会被消去。
class Solution {
public:
int hammingWeight(uint32_t n) {
int cnt = ;
while (n) {
n &= (n - );
cnt++;
} return cnt;
}
};
LeetCode 191 Number of 1 Bits的更多相关文章
- 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 二进制数1的个数
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- Java for 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 ...
- (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 ...
- Java [Leetcode 191]Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...
- [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 ...
随机推荐
- 搭建 Linux 下 GitLab 服务器
转自:http://blog.csdn.net/passion_wu128/article/details/8216086 目录: 平台需求 硬件需求 本安装指南已于 DebianUbuntu 测试通 ...
- Java Sax解析
一. Java Sax解析是按照xml文件的顺序一步一步的来解析,在解析xml文件之前,我们要先了解xml文件的节点的种类,一种是ElementNode,一种是TextNode.如下面的这段boo ...
- 【GXZ的原创】C++小游戏——五子棋
前些时候考完试自己编的带有胜负判定的五子棋. 操作方法:WSAD或↑↓←→移动下棋位置,Space或Enter放置. 如果游戏出现bug,欢迎大家在评论区反馈. #include <stdio. ...
- Hadoop之Storm命令
Hadoop之Storm命令 1.storm核心概念 stream--->一列火车 tuple--->一节车厢 数据--->乘客 spout--->始发站 bolt---> ...
- Android 如何实现带滚动条的TextView,在更新文字时自动滚动到最后一行?
1.在布局文件中放置一个TextView,给它添加scrollbars和fadeScrollbars两个属性. 如下设置:滚动条为垂直滚动条,并且一直可见(当TextView中的文字行数超过页面能显示 ...
- EasyUI中datagrid控件的使用 设置多行表头(两行或多行)
EasyUI中的datagrid控件十分强大,能生成各种复杂的报表,现在因为项目需要,需要生成一个表头两行的表,找了一些说明文档,以下用一个实例来说明一下: 第一种方法: $('#divData'). ...
- Codeforces Round #292 (Div. 2) C. Drazil and Factorial
题目链接:http://codeforces.com/contest/515/problem/C 给出一个公式例如:F(135) = 1! * 3! * 5!; 现在给你一个有n位的数字a,让你求这样 ...
- Oracle 数据库1046事件
例子: session 2: SQL> connect test/test Connected. select * from v$mystat where rownum=1; 143 selec ...
- 使用json存储结构化数据
从文件中读写字符串很容易.数值就要多费点儿周折,因为read ()方法只会返回字符串,应将其传入int()这样的函数,就可以将'123'这样的字符串转换为对应的数值 123.当你想要保存更为复杂的数据 ...
- 500 OOPS: vsftpd: both local and anonymous access
配置ftp服务器,有如下报错 C:\netos74\bin>ftp 10.20.100.252Connected to 10.20.100.252.500 OOPS: vsftpd: both ...