My Accepted Java solution. O(1) complexity.


  • 1
    O
    public class Solution {
        public boolean isPowerOfFour(int num) {
        	if (num == 0) {
        		return false;
        	}
        	if (num == 1) {
        		return true;
        	}
        	double result = Math.log((double)num)/Math.log(4);
        	return result == Math.floor(result);
        }
    }

  • 0
    H

    Very simple solution.Thanks!


  • 0
    A

    Well, I also did it with log but after reading more, realized with log solution you cannot say O(1) complexity. As the editorial solution (for power of 3) says about that as UNKNOWN in case of log.


  • 0
    O

    I suppose you mean that log function implemented with Taylor's series. Who knows how Math.log function implements in Java? I need to check it.


Log in to reply
 

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