O(n) time O(1) space solution


  • 0
    R
    class Solution {
        public boolean isPowerOfTwo(int n) {
            return n > 0 && 1073741824 % n == 0;
        }
    }
    

    This solution is based on the solution for isPowerOfThree.
    Here, first we calculate highest integer which is a power of 2, not in the solution of code but outside the scope of our answer so that it does not affect the time complexity of our solution. This number comes out to be 1073741824. Later, we just check if the given number is greater than 0 and is completely divisible by the former calculated number.


Log in to reply
 

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