My Simple Answer, idea is the same as in Power of Three


  • 0
    T

    My idea here is the same as the top voted answer in Power of Three. Observe that all odd powers of two have the last digit in 2 and 8, while all even powers of two (which are powers of 4) have the last digit in 4 and 8. 1073741824 is the largest power of 4 that is smaller than Integer.MAX_VALUE.

    public class Solution {
        public boolean isPowerOfFour(int num) {
            int lastDigit = num % 10;
            return num > 0 && (1073741824 % num == 0 && (lastDigit == 1 || lastDigit == 4 || lastDigit == 6));
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.