O(1) solution in java


  • 0
    C
    public class Solution {
        public boolean isPowerOfTwo(int n) {
            if(n==1)
                return true;
            if(n==Integer.MIN_VALUE)
                return false;
            String bint = Integer.toBinaryString(n);
            if(bint.charAt(0) != '1')
                return false;
            String bint1 = bint.substring(1);
            return !bint1.contains("1");
        }
    }
    

Log in to reply
 

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