Java Solution, 2ms


  • 0

    First I attempt I used Math.log, but returned a floating-point error.

    public class Solution {
        public boolean isPowerOfTwo(int n) {
           double n_d = (double)n;
           while(n_d >= 2){
               n_d = n_d/2.0;
           } 
           return n_d == 1.0;
        }
    }
    

Log in to reply
 

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