Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1' bits i
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.
class Solution {
public:
int hammingWeight(uint32_t n) {
int i,res=0;
for(i=0;i<32;i++)
{
if(n%2==1)
{res++;}
//return n;
n = n>>1;//这里要进行赋值,不然不会改变n的值
//return n;
}
return res;
}
};
Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1' bits i的更多相关文章
- pug.compile() will compile the Pug source code into a JavaScript function that takes a data object (called “locals”) as an argument.
Getting Started – Pug https://pugjs.org/api/getting-started.html GitHub - Tencent/wepy: 小程序组件化开发框架 h ...
- vue引入fastclick设置输入框type="number"报错Failed to execute 'setSelectionRange' on 'HTMLInputElement': The input element's type ('number') does not support selection.的解决办法
将输入框type设为text,通过正则验证输入的值
- [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
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- Number of 1 Bits(Difficulty: Easy)
题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- LeetCode 191 Number of 1 Bits
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
- leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number
一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...
- 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 ...
随机推荐
- 经验收获Linux终端下方便命令
一.命令行日常系快捷键 如下的快捷方式非常有用,能够极大的提升你的工作效率: CTRL + U - 剪切光标前的内容 CTRL + K - 剪切光标至行末的内容 CTRL + Y - 粘贴 CTRL ...
- html5 滚动小球
<html> <head> <meta charset="utf-8"/> </head> <body onkeydown=& ...
- _beginThreadex的用法
http://blog.csdn.net/cjcy1984001/article/details/6675669 线程开始和停止函数! unsigned long _beginthreadex( v ...
- java集合框架之HashSet
参考http://how2j.cn/k/collection/collection-hashset/364.html#nowhere 元素不能重复 Set中的元素,不能重复重复判断标准是: 首先看ha ...
- jQuery 设置图片 src 的2种方法
// 方法1 $('#imgValidateCode').attr("src", data.CodeUrl); // 方法2 var self = $("#refresh ...
- 移动平台unity3d优化
目录(?)[-] Focus on GPUs 着眼于GPU Good practice 优秀的实践 Sharer optimizations 着色器优化 Focus on CPUs 着眼于CPUs G ...
- 3DMAX 烘培技术
烘培是指,把光照信息渲染成贴图,而后把这个烘培后的贴图再贴回到场景中去的技术.烘培技术把光照计算的结果提前写入到了贴图中,因此在实时渲染中不需要进行耗时的光照计算,大大提高了实时渲染的效率. 烘培和渲 ...
- Unity3d与3dmax模型比例问题
1 把3dmax中1米的物体,在unity中为1厘米,所以unity中需要放大100倍才能跟3dmax中效果相同 2 unity中调整模型->inspector-scale factor可以调整 ...
- C# interface 的特性 无法被implement class继承
最近做interface添加特性后,implement class 无法继承. 微软要求class是实现Interface而不是继承,所以我们必须手动添加特性,而不能自动继承. 对于abstract ...
- 51nod1402(贪心)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1402 题意:中文题诶- 思路:贪心 对于一个桩点,如果我们只考 ...