File tree 3 files changed +36
-0
lines changed 3 files changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ // copy from JDK Integer.bitCount
3
+ /**
4
+ * Returns the number of one-bits in the two's complement binary
5
+ * representation of the specified {@code int} value. This function is
6
+ * sometimes referred to as the <i>population count</i>.
7
+ *
8
+ * @param i the value whose bits are to be counted
9
+ * @return the number of one-bits in the two's complement binary
10
+ * representation of the specified {@code int} value.
11
+ * @since 1.5
12
+ */
13
+ public static int bitCount (int i ) {
14
+ // HD, Figure 5-2
15
+ i = i - ((i >>> 1 ) & 0x55555555 );
16
+ i = (i & 0x33333333 ) + ((i >>> 2 ) & 0x33333333 );
17
+ i = (i + (i >>> 4 )) & 0x0f0f0f0f ;
18
+ i = i + (i >>> 8 );
19
+ i = i + (i >>> 16 );
20
+ return i & 0x3f ;
21
+ }
22
+
23
+ // you need to treat n as an unsigned value
24
+ public int hammingWeight (int n ) {
25
+ return bitCount (n );
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ ---
2
+ layout : solution
3
+ title : Number of 1 Bits
4
+ date : 2015-03-10 12:29:58+08:00
5
+ leetcode_id : 191
6
+ ---
7
+ {% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
8
+ {% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_ root/' }} %}
9
+ {% include {{leetcode_readme}} %}
You can’t perform that action at this time.
0 commit comments