본문 바로가기
알고리즘 풀이

231023 LeetCode 문제 풀이

by 미노킴 2023. 10. 23.

342. Power of Four

https://leetcode.com/problems/power-of-four/?envType=daily-question&envId=2023-10-23

 

1) 문제 설명

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.

 

2) 제한 사항

    • -231 <= n <= 231 - 1

 

3) 도전 과제

Could you solve it without loops/recursion?

 

4) 풀이

도전과제대로 반복문/재귀 없이 풀어보려 했으나 실패. 해당 내용이 이미 만들어진 함수를 사용하면 된다는 것인지, 아니면 그 함수 안에서도 반복문/재귀 없이 풀어야 한다는 것인지 모르겠음.

 

*도전 과제를 해결한 분이 있으실 경우 댓글 남겨주시면 감사하겠습니다.*

 

5) 소스 코드 및 결과

class Solution {
    public boolean isPowerOfFour(int n) {
        while (true){

            if(n==0){
                return false;
            }

            if(n==1){
                return true;
            }

            if(n%4==0){
                n = n/4;
                continue;
            }

            if(n%4 != 0){
                return false;
            }
        }
    }
}