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 ...
随机推荐
- php 二维数组转换成树状数组(转)
<?php/** * @param array $list 要转换的结果集 * @param string $pid parent标记字段 * @param string $level leve ...
- 八皇后问题 lua版
简单来讲就是如何在一个8x8的棋盘中放八个棋,让他们两两不能在同一行,同一列,同一斜线. 直接贴代码(出至:programming in lua 3 ) --棋盘大小 SIZE = --判断棋放在ro ...
- git 创建远程仓库
在远程服务器上$ cd /server/path/ $ git init --bare myproject.git 在本地 1> $ cd /client/path/ 运行 git init 2 ...
- 教学项目之-通过Python实现简单的计算器
教学项目之-通过Python实现简单的计算器 计算器开发需求 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/ ...
- lucene建立索引的过程
建立索引过程 用户提交数据=>solr建立索引=>调用lucene包建立索引 官方建立索引和查询索引的例子如下: http://lucene.apache.org/core/4_10_3/ ...
- WPF DataGrid 绑定DataSet数据 自动生成行号
1.绑定数据:dataGrid1.ItemsSource = dataSet.Tables[0].DefaultView; 注意:在创建DataGrid 时可以通过AutoGenerateColumn ...
- 困扰:C#.net 连接Oracle11g 不报错但是在connection时出现 ServerVersion 引发了“System.InvalidOperationException”类型的异常
今天在使用VS2008 32位 连接 64位的Oracle11g的数据库时出现 “conn.ServerVersion”引发了“System.InvalidOperationException”类型的 ...
- cxf客户端代码wsdlLocation设置相对路径
利用工生成的cxf客户端代码,wsdlLocation都是绝对路径,为了便于项目更加灵活管理,我们可以将该路径设置为相对路径: 1.下面图片是我的项目路径图片及wsdl地址存放路径: 2.下面图片是我 ...
- PHP简单计算器
工作之余,写着玩的. <?php $num1 = $_POST['num1']; $num2 = $_POST['num2']; $yusuan = $_POST['yusuan']; $jie ...
- macbook Android开发环境搭建,真机调试
买了一台MacBook,本以为可以鼓捣一下iOS开发之类的,可惜导师要我做Android开发.无奈开始了在MacBook上开发Android的工作. 从开始配置环境到应用成功在真机上运行,也是曲曲折折 ...