LeetCode Number of 1 Bits 计算1的个数

题意:
提供一个无符号32位整型uint32_t变量n,返回其二进制形式的1的个数。
思路:
考察二进制的特性,设有k个1,则复杂度为O(k)。考虑将当前的数n和n-1做按位与,就会将n的最后一个1去掉,重复这样的操作就可以统计出1的个数了。(2015年春季 小米实习生的笔试题之一)
class Solution {
public:
int hammingWeight(uint32_t n) {
int cnt=;
while(n)
{
n&=n-;
cnt++;
}
return cnt;
}
};
AC代码
python3
class Solution(object):
def hammingWeight(self, n):
"""
:type n: int
:rtype: int
"""
cnt=0
while n:
cnt+=1
n=n&(n-1)
return cnt
AC代码
LeetCode Number of 1 Bits 计算1的个数的更多相关文章
- [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 ...
- 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
原题链接在这里:https://leetcode.com/problems/number-of-1-bits/ 题目: Write a function that takes an unsigned ...
- [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 return the number of '1' bits it has (also kn ...
- [LeetCode] 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 Digit One 数字1的个数
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less th ...
- LeetCode——Number of 1 Bits
//求一个整数的二进制串中1的个数 public int hammingWeight(int n) { String b_str = Integer.toBinaryString(n); int b_ ...
- [LeetCode] Number of Distinct Islands 不同岛屿的个数
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
随机推荐
- Win7常用但是被忽略的快捷键
General keyboard shortcuts 1.Ctrl + Right Arrow Move the cursor to the beginning of the next word 向 ...
- 网页DIV+CSS布局和动画美化全程实例 (陈益材) 随书光盘
网站的建站技术近几年得到迅速的发展,网页的布局与特效动画技术层出不穷,网站建设已经从简单的技术支持时代衍变到现在的视觉美化时代.但如何使设计的网页高人一筹,达到让人过目不忘的境界,如何追求以最简单的特 ...
- asp.net Page.Controls对象(找到所有服务器控件)
前台 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="De ...
- Note: Bimodal Content Defined Chunking for Backup Streams
CDC算法给出了一个chunk的大小的最小值.最大值.平均值的界定. Method Using chunk existence information breaking-apart algorithm ...
- Codeforces Round #527 (Div. 3)D2(栈,思维)
#include<bits/stdc++.h>using namespace std;int a[200007];stack<int>s;int main(){ int ...
- 浅谈UML——九种图(一)
前言 学UML将近两个星期了,对UML有了一定的了解,学过的没学过的都知道UML中最最最核心的部分要数那九个图了.浅谈UML九种图. 实例 1.用例图: 什么是用例?描绘一个系统外在可见的需求情况,是 ...
- bzoj1934: [Shoi2007]Vote 善意的投票(最小割)
传送门 考虑源点为同意,汇点为反对,那么只要源点向同意的连边,不同意的向汇点连边,求个最小割就是答案 然后考虑朋友之间怎么办,我们令朋友之间连双向边.这样不管怎么割都能对应一种选择情况.那么还是求一个 ...
- JavaWeb:JSP技术基础
JavaWeb:JSP技术 快速开始 介绍 JSP全称Java Server Pages,是一种动态网页开发技术.它使用JSP标签在HTML网页中插入Java代码.标签通常以<%开头以%> ...
- JMETER CSS JQUERY EXTRACTOR
我想如果你在这里,你可能已经访问了我们关于变量提取的JMeter系列: XPath Extractor:使用XPath Expressions从XML响应中提取内容, Regexp Extractor ...
- chapter09
import java.io.File import java.nio.file._ import scala.collection.mutable.ArrayBuffer/** * Created ...