位运算(2)——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.
输入:32位整数
输出:二进制中1的个数
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int res = 0;
while(n != 0) {
/*if((n & 1) == 1) { //超时
res++;
n >>= 1;
} else {
n >>= 1;
}*/
n &= (n-1); //消掉n最右边的1
res++;
}
return res;
}
}
位运算(2)——Number of 1 Bits的更多相关文章
- 位运算(3)——Reverse Bits
翻转32位无符号二进制整数 Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (r ...
- [Swift]LeetCode693. 交替位二进制数 | Binary Number with Alternating Bits
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will a ...
- leetcode - 位运算题目汇总(下)
接上文leetcode - 位运算题目汇总(上),继续来切leetcode中Bit Manipulation下的题目. Bitwise AND of Numbers Range 给出一个范围,[m, ...
- BitMap - leetcode [位运算]
136. Single Number 因为A XOR A = 0,且XOR运算是可交换的,于是,对于实例{2,1,4,5,2,4,1}就会有这样的结果: (2^1^4^5^2^4^1) => ( ...
- BZOJ4245 ONTAK2015 OR-XOR 【位运算+贪心】*
BZOJ4245 ONTAK2015 OR-XOR Description 给定一个长度为n的序列a[1],a[2],…,a[n],请将它划分为m段连续的区间,设第i段的费用c[i]为该段内所有数字的 ...
- 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 ...
- Leetcode#191. Number of 1 Bits(位1的个数)
题目描述 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 000000 ...
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
- [HDU] 3711 Binary Number [位运算]
Binary Number Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
随机推荐
- kuangbin专题16B(kmp模板)
题目链接: https://vjudge.net/contest/70325#problem/B 题意: 输出模式串在主串中出现的次数 思路: kmp模板 在 kmp 函数中匹配成功计数加一, 再令 ...
- P1114 “非常男女”计划
题意:给你一个01串,求满足0和1总数相等的最大字串 $n\ \le\ 10^5$ 1.$O(n^3)$枚举起点终点,统计判断是否成立 2.$O(n^2)$先$O(n)$时间计算01个数的前缀和, ...
- 使用gifplayer操作gif的方法
使用的工具--gifplayer 基本用法: 1.安装 git clone https://github.com/rubentd/gifplayer.git 2.添加一张gif预览的图片 <im ...
- Please tell me who you are. Run git config --global user.email "you@example.com" git config --global user.name "Your Name"
解决办法
- angularJs实现修改功能
思路: 对表单中内容进行修改,首先需要获取这个内容,再进行修改,再清空弹窗中的内容 //查询实体 $scope.findOne=function(id){ $http.get('../brand/fi ...
- README.md使用
1.标题: # 一级标题 ## 二级标题 ### 三级标题 2.换行: <br> 3.代码: ``` 代码在中间 ``` 4.表格: 表头 | 表头 | 表头 ---- | ----- | ...
- 001 开发环境搭建、安卓项目结构、R文件位置、asset目录创建
1.安卓开发平台搭建 (1)下载SDK基础工具包(自己的百度云中) (2)将下载的安装包(android-sdk_r24.4.1-windows.zip)解压后,放到以下路径 C:\SoftAppli ...
- 网络流EdmondsKarp算法模板理解
先推荐一个讲网络流的博客,我的网络流知识均吸收于此 传送门 EdmondsKarp算法基本思想:从起点到终点进行bfs,只要存在路,说明存在增广路径,则取这部分路 权值最小的一部分,即为增广路径( ...
- Django ORM 字段合集
AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列,必须填入参数 primary ...
- 问题解决Determine IP information for eth0.. no link present check cable
网上方法都没有解决:简单粗暴编辑里还原了默认设置OK了 网上方法1 一般解决办法: 第一步: 到/etc/sysconfig/network-scripts/ifcfg-eth<n>/et ...