|
1 | 1 | packagecom.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/**231. Power of Two |
| 3 | +/** |
| 4 | + * 231. Power of Two |
4 | 5 | *
|
5 |
| - * Given an integer, write a function to determine if it is a power of two.*/ |
| 6 | + * Given an integer, write a function to determine if it is a power of two. |
| 7 | + */ |
6 | 8 |
|
7 | 9 | publicclass_231 {
|
8 |
| -publicbooleanisPowerOfTwo(intn) { |
9 |
| -//after writing out the binary representation of some numbers: 1,2,4,8,16,32, you can easily figure out that |
10 |
| -//every number that is power of two has only one bit that is 1 |
11 |
| -//then we can apply that cool trick that we learned from {@link easy._191}: n&(n-1) which will clear the least significant bit in n to zero |
12 |
| -returnn >0 && (n & (n -1)) ==0; |
13 |
| - } |
14 |
| - |
15 |
| -publicstaticvoidmain(String...strings) { |
16 |
| -_231test =new_231(); |
17 |
| -System.out.println(test.isPowerOfTwo(14)); |
| 10 | +publicstaticclassSolution1 { |
| 11 | +publicbooleanisPowerOfTwo(intn) { |
| 12 | +//after writing out the binary representation of some numbers: 1,2,4,8,16,32, you can easily figure out that |
| 13 | +//every number that is power of two has only one bit that is 1 |
| 14 | +//then we can apply that cool trick that we learned from {@link easy._191}: n&(n-1) which will clear the least significant bit in n to zero |
| 15 | +returnn >0 && (n & (n -1)) ==0; |
| 16 | + } |
18 | 17 | }
|
19 | 18 | }
|