(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 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.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
参考编程之美120页
方法1:使用位操作
代码如下:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int num=0;
while(n!=0){
num+=(n&1);
n=n>>>1;
}
return num;
}
}
运行结果:时间复杂度为O(logV),即二进制位数。
方法二:n&=(n-1)
代码如下:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int num=0;
while(n!=0){
n&=(n-1);
num++;
}
return num;
}
}
运行结果:
(easy)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 ...
- LeetCode 191 Number of 1 Bits
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
- 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 ...
- 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 ...
随机推荐
- python3倒叙字符串
google测试工程师的一道题: 设计一个函数,使用任意语言,完成以下功能: 一个句子,将句子中的单词全部倒排过来,但单词的字母顺序不变.比如,This is a real world,输出结果为 w ...
- class的使用
class test(object): """ get被称之为test对象的方法 """ def __init__(self,var1): ...
- $_SERVER 相关变量
PHP编程中经常需要用到一些服务器的一些资料,特把$_SERVER的详细参数整理下,方便以后使用. $_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document root ...
- Android手机平板两不误,使用Fragment实现兼容手机和平板的程序
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8744943 记得我之前参与开发过一个华为的项目,要求程序可以支持好几种终端设备,其 ...
- android学习笔记十——TabHost
TabHost——标签页 ==> TabHost,可以在窗口放置多个标签页,每个标签页相当于获得了一个与外部容器相同大小的组件摆放区域. 通过此种方式可以实现在一个容器放置更多组件(EG:通话记 ...
- java运用Comparator为对象排序
要排序的类需要实现Comparator接口,重写compare方法: user类及实现接口的内部类: package test; import java.util.Comparator; public ...
- abstract 与 interface
接口和抽象类: 最本质的区别:抽象类是一个不完全的类,是对对象的抽象,而接口是一种行为规范. a. interface中不能有字段,abstract class则可以:b. interface可以被多 ...
- C#中的 int?是什么意思
http://www.cnblogs.com/firstcsharp/archive/2011/12/11/2283797.html int?:表示可空类型,就是一种特殊的值类型,它的值可以为null ...
- 黄聪:C#如何操作JSON数据(读取、分析)
使用开源的类库Newtonsoft.Json(下载地址http://json.codeplex.com/).下载后加入工程就能用.通常可以使用JObject, JsonReader, JsonWrit ...
- (WCF) WCF Service Hosting.
3 Options. 1. Host inside of an application. 2. Host into Windows service. 3. Host into IIS 参考: http ...