JAVA - 1 line with little trick - no 0x55555555 usage


  • 0
    N

    Here is the code :

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

    Some explations:

    • 1073741824 % num == 0 : this is used to confirm that there is only 1 bit is 1 in the int, which learn from other questions (1073741824)
    • num % 10 == 4 || num % 10 == 6 : this is the trick part. if you observe all the number that is power of Four, you will find the smallest digist is either 4 or 6.

Log in to reply
 

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