|
1 | 1 | packagecom.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 unsignedintegerand returns the number of ’1' bits ithas(also known astheHamming weight). |
| 7 | + * For example, the 32-bitinteger’11'hasbinary representation 00000000000000000000000000001011, sothefunction 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 | publicclass_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 |
| -publicinthammingWeight_editorial(intn) { |
12 |
| -intcount =0; |
13 |
| -while (n !=0) { |
14 |
| -count++; |
15 |
| -n &= (n -1); |
16 |
| - } |
17 |
| -returncount; |
18 |
| - } |
19 | 10 |
|
20 |
| -publicstaticvoidmain(String...strings) { |
21 |
| -System.out.println(4 &5); |
22 |
| -_191test =new_191(); |
23 |
| -System.out.println(test.hammingWeight_editorial(5)); |
| 11 | +publicstaticclassSolution1 { |
| 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 | +publicinthammingWeight(intn) { |
| 16 | +intbits =0; |
| 17 | +while (n !=0) { |
| 18 | +bits++; |
| 19 | +n &= (n -1); |
| 20 | + } |
| 21 | +returnbits; |
| 22 | + } |
24 | 23 | }
|
25 | 24 |
|
26 |
| -// you need to treat n as an unsigned value |
27 |
| -publicinthammingWeight(intn) { |
28 |
| -//cheers! Made it AC'ed on my own with an ease! |
29 |
| -intcount =0; |
30 |
| -for (inti =0;i <32;i++) { |
31 |
| -intone = (n >>>i) &1;//must use unsigned right shift operator |
32 |
| -if (one ==1) { |
33 |
| -count++; |
| 25 | +publicstaticclassSolution2 { |
| 26 | +publicinthammingWeight(intn) { |
| 27 | +intbits =0; |
| 28 | +intmask =1; |
| 29 | +for (inti =0;i <32;i++) { |
| 30 | +if ((n &mask) !=0) { |
| 31 | +bits++; |
| 32 | + } |
| 33 | +mask <<=1; |
34 | 34 | }
|
| 35 | +returnbits; |
35 | 36 | }
|
36 |
| -returncount; |
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 |
| -publicinthammingWeight_faster(intn) { |
42 |
| -intcount =0; |
43 |
| -for (inti =0;i <32;i++) { |
44 |
| -intone = (n >>>i) &1;//must use unsigned right shift operator |
45 |
| -if (one ==1) { |
46 |
| -count++; |
47 |
| - } |
48 |
| -if (n ==0) { |
49 |
| -returncount; |
| 39 | +publicstaticclassSolution3 { |
| 40 | +publicinthammingWeight(intn) { |
| 41 | +intbits =0; |
| 42 | +for (inti =0;i <32;i++) { |
| 43 | +if ((n &1) ==1) { |
| 44 | +bits++; |
| 45 | + } |
| 46 | +if (n ==0) { |
| 47 | +returnbits; |
| 48 | + } |
| 49 | +/**must use unsigned right shift operator since the problem says this is an unsigned value*/ |
| 50 | +n >>>=1; |
50 | 51 | }
|
| 52 | +returnbits; |
51 | 53 | }
|
52 |
| -returncount; |
53 | 54 | }
|
54 |
| - |
55 | 55 | }
|