Easy figure out java solution


  • 0
    S
    public class Solution {
        public boolean isPowerOfTwo(int n) {
            if(n <= 0) return false;
            int cnt = 0;
            int tmp = n;
            while(tmp != 1) {
                tmp = tmp >> 1;
                cnt++;
            }
            tmp = 1 << cnt;
            if((tmp|n) == tmp) return true;
            return false;
        }
    }

Log in to reply
 

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