릿코드(LEETCODE)

326. Power of Three

cepiloth 2020. 2. 8. 17:35
반응형

 

https://leetcode.com/problems/power-of-three

불러오는 중입니다...

 

3의 제곱수인지 확인 하는 문제

3으로 계속 나누어 나머지가 있으면 실패 그외의 경우 에러 처리함

class Solution {
public:
    bool isPowerOfThree(int n) {
        
        // 이처리가 마음에 안듦
        if(n == 1) {
            return true;
        }
        
        // 이처리가 마음에 안듦
        if(n == 0) {
            return false;
        }
        
        while(true) {
            int cand = n % 3;
            
            // 나머지 있으면 3의 배수가 아니니 ㅂㅂ2
            if(cand != 0)
                return false;
            
            n = n / 3;
            
            if(n == 1) {
                break;
            }
        }
        
        return true;
    }
};

 

허어 Solution 에 아래와 같은 코드가 두개 있는데 이해가 안간다...

 

public class Solution {
    public boolean isPowerOfThree(int n) {
        return n > 0 && 1162261467 % n == 0;
    }
}

 

public class Solution {
    public boolean isPowerOfThree(int n) {
        return (Math.log10(n) / Math.log10(3)) % 1 == 0;
    }
}

 

반응형