(LeetCode 342) Power of Four

Q:
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example:
Given num = 16, return true. Given num = 5, return false.

Follow up: Could you solve it without loops/recursion?

solution:
解法很多,思路和之前的方法一樣:
(LeetCode 326)Power of Three

(LeetCode 231)Power of Two

4x=n
x=log(n)/log(4)
判斷x是否爲整數

class Solution {
public:
    bool isPowerOfFour(int num) {
        double x = log10(num)/log10(4);
        if(x==int(x))return true;
        return false;
    }
};
發佈了45 篇原創文章 · 獲贊 15 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章