N

(Leet Code c++)Power of Four 본문

Leet Code 알고리즘

(Leet Code c++)Power of Four

naeunchan 2021. 8. 3. 10:39
728x90
반응형

 

342. Power of Four

 

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.

 

Example 1:

Input: n = 16 Output: true

Example 2:

Input: n = 5 Output: false

Example 3:

Input: n = 1 Output: true

 

Constraints:

  • -231 <= n <= 231 - 1

 

https://eunchanee.tistory.com/527

 

(Leet Code c++)Power of Three

326. Power of Three Given an integer n, return true if it is a power of three. Otherwise, return false. An integer n is a power of three, if there exists an integer x such that n == 3x. Ex..

eunchanee.tistory.com

위 문제와 동일한 알고리즘.

class Solution {
public:
    bool isPowerOfFour(int n) {
        if(n <= 0){
            return false;
        }
        
        while(n != 1){
            if(n % 4){
                return false;
            }
            n /= 4;
        }
        
        return true;
    }
};
728x90
반응형