|
1 | 1 | package com.fishercoder.solutions;
|
2 |
| -/**191. Number of 1 Bits |
| 2 | + |
| 3 | +/** |
| 4 | + * 191. Number of 1 Bits |
| 5 | + * Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). |
3 | 6 | *
|
4 |
| -Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). |
| 7 | + * For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.*/ |
5 | 8 |
|
6 |
| -For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3.*/ |
7 | 9 | public class _191 {
|
8 |
| - //another cool trick that I learned: doing bitwise AND operation between n and n-1 will always flip the least significant 1 bit in n |
9 |
| - //to zero, here's the solution from Editorial: |
10 |
| - //example run for the above editorial solution: input 5, n will be 5&4 and becomes 4, then in the next run, n will become 4&3 which is 0, thus exit the while loop. |
11 |
| - public int hammingWeight_editorial(int n) { |
12 |
| - int count = 0; |
13 |
| - while (n != 0) { |
14 |
| - count++; |
15 |
| - n &= (n - 1); |
16 |
| - } |
17 |
| - return count; |
18 |
| - } |
19 | 10 |
|
20 |
| - public static void main(String... strings) { |
21 |
| - System.out.println(4 & 5); |
22 |
| - _191 test = new _191(); |
23 |
| - System.out.println(test.hammingWeight_editorial(5)); |
| 11 | + public static class Solution1 { |
| 12 | + /**Doing bitwise AND operation between n and n-1 will always flip the least significant 1 bit in n to zero |
| 13 | + example run for the above editorial solution: input 5, n will be 5&4 and becomes 4, |
| 14 | + then in the next run, n will become 4&3 which is 0, thus exit the while loop.*/ |
| 15 | + public int hammingWeight(int n) { |
| 16 | + int bits = 0; |
| 17 | + while (n != 0) { |
| 18 | + bits++; |
| 19 | + n &= (n - 1); |
| 20 | + } |
| 21 | + return bits; |
| 22 | + } |
24 | 23 | }
|
25 | 24 |
|
26 |
| - // you need to treat n as an unsigned value |
27 |
| - public int hammingWeight(int n) { |
28 |
| - //cheers! Made it AC'ed on my own with an ease! |
29 |
| - int count = 0; |
30 |
| - for (int i = 0; i < 32; i++) { |
31 |
| - int one = (n >>> i) & 1;//must use unsigned right shift operator |
32 |
| - if (one == 1) { |
33 |
| - count++; |
| 25 | + public static class Solution2 { |
| 26 | + public int hammingWeight(int n) { |
| 27 | + int bits = 0; |
| 28 | + int mask = 1; |
| 29 | + for (int i = 0; i < 32; i++) { |
| 30 | + if ((n & mask) != 0) { |
| 31 | + bits++; |
| 32 | + } |
| 33 | + mask <<= 1; |
34 | 34 | }
|
| 35 | + return bits; |
35 | 36 | }
|
36 |
| - return count; |
37 | 37 | }
|
38 | 38 |
|
39 |
| - //then I turned to its Editorial solution: we can make it a little faster: at any time, when n becomes zero, that means there's |
40 |
| - //no more 1's there, then we could safely return! Cool! |
41 |
| - public int hammingWeight_faster(int n) { |
42 |
| - int count = 0; |
43 |
| - for (int i = 0; i < 32; i++) { |
44 |
| - int one = (n >>> i) & 1;//must use unsigned right shift operator |
45 |
| - if (one == 1) { |
46 |
| - count++; |
47 |
| - } |
48 |
| - if (n == 0) { |
49 |
| - return count; |
| 39 | + public static class Solution3 { |
| 40 | + public int hammingWeight(int n) { |
| 41 | + int bits = 0; |
| 42 | + for (int i = 0; i < 32; i++) { |
| 43 | + if ((n & 1) == 1) { |
| 44 | + bits++; |
| 45 | + } |
| 46 | + if (n == 0) { |
| 47 | + return bits; |
| 48 | + } |
| 49 | + /**must use unsigned right shift operator since the problem says this is an unsigned value*/ |
| 50 | + n >>>= 1; |
50 | 51 | }
|
| 52 | + return bits; |
51 | 53 | }
|
52 |
| - return count; |
53 | 54 | }
|
54 |
| - |
55 | 55 | }
|
0 commit comments