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 ...
随机推荐
- 【mysql格式化日期】
date_format(now(),'%Y-%c-%d'): 1. DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据. DATE_FORMAT(date,format) format ...
- 【lunux-make: *** 没有规则可以创建目标“install”。 停止。问题】
[root@localhost nginx-1.8.0]# make installmake: *** 没有规则可以创建目标“install”. 停止.
- java线程基础知识----线程基础知识
不知道从什么时候开始,学习知识变成了一个短期记忆的过程,总是容易忘记自己当初学懂的知识(fuck!),不知道是自己没有经常使用还是当初理解的不够深入.今天准备再对java的线程进行一下系统的学习,希望 ...
- 《OD大数据实战》Mahout入门实例
一.环境搭建 1. 下载 mahout-0.9-cdh5.3.6.tar.gz 2. 解压 3. mahout org.apache.mahout.clustering.syntheticcontro ...
- 解读人:李琼,Metabolic profiling of tumors, sera and skeletal muscles from an orthotopic murine model of gastric cancer associated-cachexia(胃癌相关恶病质的原位小鼠模型中肿瘤,血清和骨骼肌的代谢谱分析)
发表时间:(2019年4月) IF:3.950 单位: 厦门大学 厦门理工大学 物种:小鼠的肿瘤,血清和骨骼肌 技术:核磁共振代谢组学分析 一. 概述:(用精炼的语言描述文章的整体思路及结果) 本研究 ...
- springboot jpa mongodb 多条件分页查询
public Page<Recorded> getRecordeds(Integer page, Integer size, Recorded recorded) { if (page&l ...
- thinkphp5.1跨模块调用控制器或者模型
tp5.1 采用命名空间的方式进行调用.
- 实现网上大神的asp.net mvc + ef +easyui
大神开源博客: http://www.cnblogs.com/ymnets/p/3424309.html 系统更换UI:本人喜欢基于bootstrap的adminlteUI,所以后面会将UI更换为ad ...
- Unity 行为树-共享变量
一.引言 有以下小场景: 节点A:发现了 敌人. 节点B:追逐敌人. 对于同一个敌人物体,节点AB之间是如何传递数据 的呢? 行为树节点AB之间,需要一个中间变量Temp来传递数据. A发现了敌人,将 ...
- 自动化测试资源(一):谷歌浏览器驱动 ChromeDriver
ChromeDriver(官网):https://sites.google.com/a/chromium.org/chromedriver/ (需要XX上网,官网里有驱动和浏览器的版本映射关系) (如 ...