LeetCode Number of 1 Bits
原题链接在这里:https://leetcode.com/problems/number-of-1-bits/
题目:
Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight).
Example 1:
Input: 00000000000000000000000000001011
Output: 3
Explanation: The input binary string00000000000000000000000000001011 has a total of three '1' bits.
Example 2:
Input: 00000000000000000000000010000000
Output: 1
Explanation: The input binary string 00000000000000000000000010000000 has a total of one '1' bit.
Example 3:
Input: 11111111111111111111111111111101
Output: 31
Explanation: The input binary string 11111111111111111111111111111101 has a total of thirty one '1' bits.
Note:
- Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
- In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3 above the input represents the signed integer
-3.
Follow up:
If this function is called many times, how would you optimize it?
题解:
Bit manipulation每次右移一位 & 1.
unsigned number 右移 >>>. signed number 右移>>.
也可以使用Integer.bitCount() function.
Time Complexity: O(1). Space: O(1).
AC Java:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
//Method 1
// int count = 0;
// for(int i = 0; i<32; i++){
// count += (n>>>i)&1;
// }
// return count;
//Method 2
int count = 0;
while(n != 0){
count += (n&1);
n = n>>>1;
}
return count;
}
}
Method 3 是通过 n & (n-1)消掉最右侧的一个1.
消掉一个1, 对应的res就加一。
AC Java:
public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
//Method 3
int res = 0;
while(n!=0){
n = n & n-1;
res++;
}
return res;
}
}
跟上Hamming Distance, Counting Bits.
LeetCode Number of 1 Bits的更多相关文章
- 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 ...
- [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 (also know ...
- LeetCode——Number of 1 Bits
//求一个整数的二进制串中1的个数 public int hammingWeight(int n) { String b_str = Integer.toBinaryString(n); int b_ ...
- LeetCode Number of 1 Bits 计算1的个数
题意: 提供一个无符号32位整型uint32_t变量n,返回其二进制形式的1的个数. 思路: 考察二进制的特性,设有k个1,则复杂度为O(k).考虑将当前的数n和n-1做按位与,就会将n的最后一个1去 ...
- lc面试准备:Number of 1 Bits
1 题目 Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- 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 ...
随机推荐
- iphone H5 input type="search" 不显示搜索 解决办法
H5 input type="search" 不显示搜索 解决办法 H5 input type="search" 不显示搜索 解决方法 在IOS(ipad iP ...
- Maven_根据不同个环境打包, 获取不同的配置文件等等
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- 20145330第六周《Java学习笔记》
20145330第六周<Java学习笔记> . 这周算是很忙碌的一周.因为第六周陆续很多实验都开始进行,开始要准备和预习的科目日渐增多,对Java分配的时间不知不觉就减少了,然而第十和十一 ...
- Ajax注册验证js代码
分享jquery网站:http://www.css88.com/jqapi-1.9/focusout/ $(document).ready(function() { var bool_user = f ...
- 转载:C# this.Invoke()的作用与用法 理解三
Invoke()的作用是:在应用程序的主线程上执行指定的委托.一般应用:在辅助线程中修改UI线程( 主线程 )中对象的属性时,调用this.Invoke(); 在多线程编程中,我们经常要在工作线程 ...
- java mail(发送邮件--163邮箱)
package com.util.mail; /** * 发送邮件需要使用的基本信息 */ import java.util.Properties; public class MailSenderIn ...
- 关于ASPCMS标签调用的一些总结
菜单的应用 <ul class="nav navbar-nav"> {aspcms:navlist} {}<!--判断是否有下级目录--> <li c ...
- 配置DNS实验一例
1安装bind软件 2查看当前DNS服务 3修改配置文件 4测试
- 怎么在Centos7下添加win8.1的启动项
首先找到启动文件. 在/boot/grub2目录下, 找到grub.cfg文件. 然后, sudo修改, 用gedit工具方便. 怎么修改? 打开文件, 找到有两个menuentry开头的部分, 然 ...
- 20145337实验三实验报告——敏捷开发与XP实践
20145337实验三实验报告--敏捷开发与XP实践 实验名称 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 ** 实验步骤**### 敏捷开发与XP 软件工程包括下列领域:软件需求 ...