Number of 1 Bits——LeetCode
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.
题目大意:给一个无符号数,返回这个数二进制表示中1的数量。
解题思路:按位和0x1与操作,计算即可。
public int hammingWeight(int n) {
int res = 0;
while(n!=0){
if((n&0x1)==1){
res++;
}
n=n>>>1;
}
return res;
}
Number of 1 Bits——LeetCode的更多相关文章
- 191. Number of 1 Bits Leetcode Python
Write a function that takes an unsigned integer and returns the number of '1' bits it has (also know ...
- 693. Binary Number with Alternating Bits - LeetCode
Question 693. Binary Number with Alternating Bits Solution 思路:输入一个整数,它的二进制01交替出现,遍历其二进制字符串,下一个与上一个不等 ...
- [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
原题链接在这里: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] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- [LeetCode] Binary Number with Alternating Bits 有交替位的二进制数
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- 【一天一道LeetCode】#191. Number of 1 Bits
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Write a ...
- 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 ...
随机推荐
- sparkSQL1.1入门之二:sparkSQL执行架构
在介绍sparkSQL之前.我们首先来看看,传统的关系型数据库是怎么执行的.当我们提交了一个非常easy的查询: SELECT a1,a2,a3 FROM tableA Where con ...
- php-fpm 启动参数及重要配置详解<转>
原文地址 http://levi.cg.am/archives/3127 约定几个目录 /usr/local/php/sbin/php-fpm /usr/local/php/etc/php-fpm. ...
- codevs 1281 Xn数列 (矩阵乘法)
/* 再来个题练练手 scanf longlong 有bug....... */ #include<cstdio> #include<iostream> #include< ...
- ASP.NET-FineUI开发实践-17
我又不用FineUI开发,所以FineUI项目经验等于0,最近在忙别的,所以也没工夫研究.积累了论坛和群里的问题,写下来留个备份 1.在grid可编辑单元格中,如果需要在点击该单元格时,单元格中所有文 ...
- MemCache缓存和C#自带的Cache缓存
1.MemCache: //初始化 static SockIOPool _pool; // 创建Memcached private static MemcachedClient Create(stri ...
- exp、imp简单测试
imp 分为以下几个测试场景 imp name1/password1 file=xxxx.dmp full=y fromuser=name2 touser=name3 场景1 name1正确.pas ...
- 【转】 UITableView 的indexPath
原文:http://blog.csdn.net/mengtnt/article/details/6733691 前面说过了viewController的一些基本注意事项.这里针对不同的viewCont ...
- iOS加载启动图的时候隐藏statusbar + 指定启动图显示多少秒
只需需要在info.plist中加入Status bar is initially hidden 设置为YES 补充一下,现在手机越来越快,在6+下面启动图一闪而过,而美工童鞋辛辛苦苦做的图就看不到鸟 ...
- Hibernate中分页
query.setFirstResult(4);query.setMaxResults(5); 这两个方法就是hibernate的分页
- JVM内存管理及垃圾回收
一.JVM内存的构 Java虚拟机会将内存分为几个不同的管理区,这些区域各自有各自的用途,根据不同的特点,承担不同的任务以及在垃圾回收时运用不同的算法.总体分为下面几个部分: 程序计数器(Progra ...