[191] Number of 1 Bits
2016-06-27 20:23
134 查看
1. 题目描述
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.
读题可真是废了大劲了,一开始没懂是什么意思,看了看Hamming weight的wiki,更晕了。后来一搜,原来是给定一个整数(被看做无符号整数),统计这个无符号整数的二进制表示中有多少个1。
2. 解题思路
第一种想法,最简单的想法,将这个int值直接转换为字符串,统计字符串中1的个数,如Code1,这个方法效率不太高。第二种想法,通过位移操作,每次用过按位与看最后一位是否为1,如果是1,那么就统计加1。这种方法由于有符号数(pretend to be unsigned)的特性坑比较多。
第三种就是比较牛逼的了,一次移动好几位,最好使用32能整除的位数,比如2,4,8,16之类的,这样移动起来比较方便。
3. Code
// Code1:效率很低 public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { // 将数字转为二进制字符串 String s = Integer.toBinaryString(n); int result = 0; for (int i = 0; i < s.length(); ++i) { // 如果字符串中有字符1 if (s.charAt(i) == '1') { // 结果个数加1 ++result; } } return result; } }
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { // 第一坑,约定的无符号数并不代表这个数大于1 /*if (n <= 0) { return 0; }*/ int result = 0; // 第二坑,MIN_VALUE的abs值是0,而本来的设定是移到0时退出,返回结果 while(Math.abs(n) > 0 || n == Integer.MIN_VALUE) { if ((n & 1) == 1) { ++result; } // 第三坑,既然符号位也算到统计中,需要使用带符号右移的操作 n >>>= 1; // 带符号右移一位,符号位跟着一起向右移动 } return result; } }
// 附上Math.abs(int value) public static int abs(int a) { return (a < 0) ? -a : a; }
// 最后附一个牛人发明的效率更高的方法 public class Solution { // you need to treat n as an unsigned value private static final int[] ARR = new int[]{0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4}; public int hammingWeight(int n) { int result = 0; while(Math.abs(n) > 0 || n == Integer.MIN_VALUE) { result+=ARR[n & 0xF]; n >>>= 4; // 一下子移动4位,效率变高,喜欢的话,可以填个2048的,这样计算个3次就够了:) } return result; } }
相关文章推荐
- C/C++位操作实例总结
- shell 中数学计算总结
- C++位操作的常见用法小结
- PHP几个数学计算的内部函数学习整理
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode