Given an integer n, return true if it is a power of four. Otherwise, return false.
An integer n is a power of four, if there exists an integer x such that n == 4x.
https://leetcode.com/problems/power-of-four/
Power of Four - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
아직까지 조금 헤매기는 하지만 그래도 실력이 늘고있다고 믿고있다 ㅎㅎ
하지만 다른사람들의 풀이는 진짜 놀라울 따름이다..
public static boolean isPowerOfFour(int n) {
if (n==0) return false;
double x;
double doubleN = n;
x = (Math.log10(doubleN) / Math.log10(4));
System.out.println("x = "+ x);
if(isNumeric(x) == true) {
return true;
}
else
return false;
}
public static boolean isNumeric(double x) {
if(Math.ceil(x) == Math.floor(x)) return true;
else return false;
}
double x가 정수인지 아닌지를 판별하는 방법을 생각하던 와중에 다양한 방법을 제시한 사이트를 찾았다. 이 사람들 처럼 창의적인 아이디어가 마구 떠올랐으면 좋겠다.ㅎㅎ
'Algorithm > LeetCode' 카테고리의 다른 글
LeetCode 733. Flood Fill (0) | 2022.07.09 |
---|---|
LeetCode 116. Populating Next Right Pointers in Each Node (0) | 2022.07.07 |
LeetCode 617. Merge Two Binary Trees (0) | 2022.07.06 |
LeetCode 763. Partition Labels (0) | 2022.07.04 |
LeetCode 22. Generate Parentheses (0) | 2022.06.30 |