Skip to content

Commit 5dfbb1d

Browse files
refactor 342
1 parent 86f9326 commit 5dfbb1d

File tree

1 file changed

+37
-49
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+37
-49
lines changed
Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,50 @@
11
package com.fishercoder.solutions;
2-
/**342. Power of Four
3-
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
42

5-
Example:
6-
Given num = 16, return true. Given num = 5, return false.
7-
8-
Follow up: Could you solve it without loops/recursion?*/
3+
/**
4+
* 342. Power of Four
5+
*
6+
* Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
7+
*
8+
* Example:
9+
* Given num = 16, return true. Given num = 5, return false.
10+
* Follow up: Could you solve it without loops/recursion?
11+
* */
912
public class _342 {
10-
//with my original idea in the bottom, just dive a little bit deeper, you can realize that another important feature of a number
11-
//that is power of four is that its only single one bit must appear on the odd position, and power of two won't meet this requirement
12-
//decimal number 8 has binary format: 0000-0000-0000-0000-0000-0000-0000-1000
13-
//decimal number 16 has binary format: 0000-0000-0000-0000-0000-0000-0001-0000
14-
//hex number 0x55555555 has binary format: 1010-1010-1010-1010-1010-1010-1010-1010
15-
//thus, doing AND with 0x55555 will check if the only one bit is located on the odd position, thus ruling out those that are power of 2 but not power of 4
16-
public boolean isPowerOfFour_bit_manipulation(int num) {
17-
return (num > 0 && 1073741824 % num == 0 && (num & 0x55555555) != 0);
13+
public static class Solution1 {
14+
//Just dive a little bit deeper, you can realize that another important feature of a number
15+
//that is power of four is that its only single one bit must appear on the odd position, and power of two won't meet this requirement
16+
//decimal number 8 has binary format: 0000-0000-0000-0000-0000-0000-0000-1000
17+
//decimal number 16 has binary format: 0000-0000-0000-0000-0000-0000-0001-0000
18+
//hex number 0x55555555 has binary format: 1010-1010-1010-1010-1010-1010-1010-1010
19+
//thus, doing AND with 0x55555 will check if the only one bit is located on the odd position, thus ruling out those that are power of 2 but not power of 4
20+
public boolean isPowerOfFour(int num) {
21+
return (num > 0 && 1073741824 % num == 0 && (num & 0x55555555) != 0);
22+
}
1823
}
1924

20-
public boolean isPowerOfFour_base_conversion(int num) {
21-
//^ means to match the beginning of a line
22-
//$ means to match the end of a line
23-
//* means zero or more of the preceding character
24-
return Integer.toString(num, 4).matches("^10*$");
25+
public static class Solution2 {
26+
public boolean isPowerOfFour(int num) {
27+
//^ means to match the beginning of a line
28+
//$ means to match the end of a line
29+
//* means zero or more of the preceding character
30+
return Integer.toString(num, 4).matches("^10*$");
31+
}
2532
}
2633

27-
//a regular loop method to make it AC'ed
28-
public boolean isPowerOfFour(int num) {
29-
if (num < 4 && num != 1) {
30-
return false;
31-
}
32-
while (num != 1) {
33-
if (num % 4 != 0) {
34+
public static class Solution3 {
35+
//a regular loop method to make it AC'ed
36+
public boolean isPowerOfFour(int num) {
37+
if (num < 4 && num != 1) {
3438
return false;
3539
}
36-
num /= 4;
37-
}
38-
return true;
39-
}
40-
41-
//simply using the max number possible that is power of 4 won't work for this case, because, that number is a power of 2, but might
42-
//not be a power of 4, e.g. number 8
43-
public boolean isPowerOfFour_not_accepted(int num) {
44-
return (num > 3 && 1073741824 % num == 0);
45-
}
46-
47-
public static void main(String... strings) {
48-
int temp = 4;
49-
int maxPowerOf4 = 4;
50-
while (temp > 0) {
51-
temp *= 4;
52-
if (temp > 0) {
53-
maxPowerOf4 = temp;
40+
while (num != 1) {
41+
if (num % 4 != 0) {
42+
return false;
43+
}
44+
num /= 4;
5445
}
46+
return true;
5547
}
56-
System.out.println("maxPowerOf4 is: " + maxPowerOf4);
57-
58-
59-
System.out.println(Integer.parseInt("55555555", 16));
60-
System.out.println(Integer.toBinaryString(Integer.parseInt("55555555", 16)));
6148
}
49+
6250
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy