Skip to content

Commit b8fd747

Browse files
authored
2022-08-26 update: added "Reordered Power of 2" (#74)
1 parent dd9cd69 commit b8fd747

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.smlnskgmail.jaman.leetcodejava.medium;
2+
3+
import java.util.Arrays;
4+
5+
// https://leetcode.com/problems/reordered-power-of-2/
6+
public class ReorderedPowerOf2 {
7+
8+
private final int input;
9+
10+
public ReorderedPowerOf2(int input) {
11+
this.input = input;
12+
}
13+
14+
public boolean solution() {
15+
int[] arr = count(input);
16+
for (int i = 0; i < 31; i++) {
17+
if (Arrays.equals(arr, count(1 << i))) {
18+
return true;
19+
}
20+
}
21+
return false;
22+
}
23+
24+
private int[] count(int n) {
25+
int[] result = new int[10];
26+
while (n > 0) {
27+
result[n % 10]++;
28+
n /= 10;
29+
}
30+
return result;
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.smlnskgmail.jaman.leetcodejava.medium;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertTrue;
6+
7+
public class ReorderedPowerOf2Test {
8+
9+
@Test
10+
public void defaultTest() {
11+
assertTrue(new ReorderedPowerOf2(1).solution());
12+
}
13+
14+
}

0 commit comments

Comments
 (0)