Skip to content

Commit 0da7036

Browse files
committed
issue #20 326-Done💯
1 parent 60b0b11 commit 0da7036

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package leetcodeEasyLevel;
2+
3+
public class _0326PowerofThree {
4+
public static boolean isPowerOfThree(int n) {
5+
//3의 배수인지 불리언타입으로 출력하기
6+
//sol1. 구글링해도 내 코드가 제일 Runtime빠른듯
7+
//Runtime 11 ms Memory 39.3 MB
8+
/*
9+
if(n == 0) return false;
10+
if(n == 1) return true;
11+
12+
while(n >= 3){
13+
if(n == 3) return true;
14+
if(n%3 == 0){
15+
n = n/3;
16+
}else{
17+
return false;
18+
}
19+
}
20+
return false;
21+
*/
22+
23+
//sol2 Runtime 22 ms Memory 43.6 MB
24+
if(n == 0) return false;
25+
while(n % 3 == 0) {
26+
n/=3;
27+
}
28+
return n == 1;
29+
}
30+
31+
public static void main(String[] args) {
32+
System.out.println(isPowerOfThree(27)+", ans: true");
33+
System.out.println(isPowerOfThree(0)+", ans: false");
34+
System.out.println(isPowerOfThree(9)+", ans: true");
35+
System.out.println(isPowerOfThree(45)+", ans: false");
36+
System.out.println(isPowerOfThree(1)+", ans: true");
37+
System.out.println(isPowerOfThree(3)+", ans: true");
38+
}
39+
}

0 commit comments

Comments
 (0)