LeetCode笔记:191. Number of 1 Bits
来源:互联网 发布:Unity3d添加GameObject 编辑:程序博客网 时间:2024/06/10 06:52
问题:
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’bits的个数(也称为Hamming weight)。
比如,32位整型数‘11’二进制表示为00000000000000000000000000001011,所以函数应该返回3。
思路:
题目的意思其实就是问一个无符号整型数的二进制形式中有多少个1。这里无符号的意思是没有负数都是正数,直接的思路就是将它转换成二进制后一个个数里面1的个数,很简单。
代码(Java):
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { String binaryStr = Integer.toBinaryString(n); int result = 0; for (int i = 0; i < binaryStr.length(); i++) { if (binaryStr.charAt(i) == '1') result++; } return result; }}
他山之石:
public static int hammingWeight(int n) { int ones = 0; while(n!=0) { ones = ones + (n & 1); n = n>>>1; } return ones;}
按照我的方法做完提交后虽然过了,也只有4ms,但是在结果统计中依然算慢的了,应该是转换成二进制那一步耗时了,然后一般数字都会有很多零,而我都会一个个判断一次,其实就不必要了。看了看Discuss中的好方法,确实挺好,直接让n和1去按位与,如果n的最后一位是1就会结果加一,然后将n右移一位继续判断,这里注意用到的右移运算发是三个箭头“>>>”,这是因为这是无符号数的右移运算符,有符号数就是两个箭头“>>”。同时循环的结束条件是n为0,这就免去了很多多余的判断,确实很赞。
合集:https://github.com/Cloudox/LeetCode-Record
版权所有:http://blog.csdn.net/cloudox_
- LeetCode笔记:191. Number of 1 Bits
- leetcode笔记:Number of 1 Bits
- 191. Number of 1 Bits Leetcode Python
- 【LeetCode】191.Number of 1 Bits
- [LeetCode]191.Number of 1 Bits
- LeetCode 191. Number of 1 Bits
- LeetCode 191. Number of 1 Bits
- [leetcode] 191. Number of 1 Bits
- <LeetCode OJ> 191. Number of 1 Bits
- Leetcode 191. Number of 1 Bits
- leetCode 191. Number of 1 Bits
- 191. Number of 1 Bits LeetCode
- Leetcode: 191. Number of 1 Bits(JAVA)
- LeetCode 191. Number of 1 Bits
- [LeetCode]191. Number of 1 Bits
- leetcode 191. Number of 1 Bits
- LeetCode *** 191. Number of 1 Bits
- [LeetCode]191. Number of 1 Bits
- iOS 关于推送点击推送通知的处理
- 真的会读书吗?
- SQLite外键(Foreign Key) 的使用例子
- 两个变量交换数值,不引入第三变量
- 我的第四个C语言:计算圆柱体的表面积和体积。
- LeetCode笔记:191. Number of 1 Bits
- angular 1.5.+ 语法理解(1)
- 论软件开发工程师的自我修养
- :read-only选择器
- 顺序表应用4-2:元素位置互换之逆置算法(数据改进)
- 关于 supervisor 的 autorestart 为 unexpected
- 项目二计算长方形的周长
- 正则表达式的用法
- 动态规划解决最长公共子序列LCS问题