Skip to content

Commit 3418746

Browse files
committed
2의 배수검사
1 parent cb39658 commit 3418746

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: PowerOfTwo.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class PowerOfTwo {
2+
3+
// 반복문 사용
4+
public boolean isPowerOfTwo(int n) {
5+
if (n <= 0) return false;
6+
if (n == 1) return true;
7+
while (n > 1) {
8+
if (n % 2 == 1)
9+
return false;
10+
n /= 2;
11+
}
12+
return true;
13+
}
14+
15+
/*
16+
// 재귀 사용
17+
public boolean isPowerOfTwo(int n) {
18+
19+
if(n==1) return true ;
20+
else if ( n%2==1 || n==0 ) return false ;
21+
22+
return isPowerOfTwo(n/2);
23+
24+
}
25+
*/
26+
}

0 commit comments

Comments
 (0)