[leetCode] Power of Two

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

public class Solution {
    public boolean isPowerOfTwo(int n) {
        if (n <= 0) return false;
        int nn = n;
        int p = 0;
        while (nn != 1) {
            nn >>= 1;
            p++;
        }

        if (1 << p == n) return true;
        return false;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章