728x90
package com.codestates.coplit;
import java.util.*;
public class Solution {
public boolean powerOfTwo(long num) {
//2의 n승이면 true
if (num == 1) return true;
//2의 n승이 아니면 false 리턴
if (num % 2 != 0) return false;
//2의 n승을 구해주는 powered
long powered = 2;
while (powered < num) {
powered = powered * 2;
}
return powered == num;
}
}
728x90
'Java > 연습문제' 카테고리의 다른 글
[JAVA] 브라우저 앞,뒤로 가기 (0) | 2022.10.21 |
---|---|
[JAVA] 문자열의 첫 글자 구하기 (0) | 2022.10.21 |
[JAVA] Json 페어 문제 (0) | 2022.10.21 |
[JAVA] 원금 2배되는 기간 (0) | 2022.10.21 |
[JAVA] hashmap을 사용해 배열의 첫 요소와 마지막 요소 받기 (0) | 2022.10.21 |
댓글