forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1780.java
28 lines (26 loc) · 796 Bytes
/
_1780.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _1780 {
public static class Solution1 {
public boolean checkPowersOfThree(int n) {
List<Integer> powers = new ArrayList<>();
int power = 1;
for (int i = 1; power <= n; i++) {
powers.add(power);
power = (int) Math.pow(3, i);
}
int i = powers.size() - 1;
while (n > 0 && i >= 0) {
if (n - powers.get(i) > 0) {
n -= powers.get(i--);
} else if (n - powers.get(i) == 0) {
return true;
} else {
i--;
}
}
return n == 0;
}
}
}