231. Power of Two


Given an integer, write a function to determine if it is a power of two.

class Solution {
    public boolean isPowerOfTwo(int n) {
        if(n == 0) return false;
        while(n%2 == 0){
            n = n/2;
        }
        return n==1;
    }
}

This site Original article All followed" Attribution—NonCommercial—ShareAlike 4.0 (CC BY-NC-SA 4.0) ”。 Please keep the following marks for sharing and interpretation:

Original author: Jake Tao Source: 「231. Power of Two」

Praise 147
0 0 147

Further reading

Post a reply

Log in can only be commented on later
Share this page
Back to top