1314. Power of Two

描述

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

您在真實的面試中是否遇到過這個題?  

無難度題目

class Solution {
public:
    /**
     * @param n: an integer
     * @return: if n is a power of two
     */
    bool isPowerOfTwo(int n) {
        // Write your code here
        while(n%2==0) n=n/2;
        if(n==1) return true;
        else return false;
    }
};

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