My concise java solution


  • 0
    J

    I don't know why it is still very slow compare to other solution. Any other good method??? so confuseddd

     if(n<=0)  return false;
            while(n%2==0){
                n/=2;
            }
            return n==1;

  • 1
    R

    Your method runs in O(lgn) time, where n is the number of bits used to represent the number. Try using bit-wise operation then you can reduce the time complexity down to O(1), which is faster.


  • 0
    J

    Yep, you are right, thanks a lot. Dude


Log in to reply
 

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