LeetCode 231 Power of Two AND LeetCode 326 Power of Three
2017-08-01 21:34
393 查看
231 Power of Two
给一个数,要求在不用循环不用递归的前提下判断这个是时候是2的某次幂。
根据一个数学公式:
LOG a ( b ) = LOG c ( b ) / LOG c ( a )假如n为2的幂,则log2(n)应为整数,套用上公式,利用log10()函数,写出判断函数:
class Solution {
public:
bool isPowerOfTwo(int n) {
return !fmod(log10(n)/log10(2), 1);
}
};
326 Power of Three
题意同上题,不过是判断是否为3的幂,同理,使用相同套路。
代码如下:
class Solution {
public:
bool isPowerOfThree(int n) {
return !fmod(log10(n) / log10(3), 1);
}
};
给一个数,要求在不用循环不用递归的前提下判断这个是时候是2的某次幂。
根据一个数学公式:
LOG a ( b ) = LOG c ( b ) / LOG c ( a )假如n为2的幂,则log2(n)应为整数,套用上公式,利用log10()函数,写出判断函数:
class Solution {
public:
bool isPowerOfTwo(int n) {
return !fmod(log10(n)/log10(2), 1);
}
};
326 Power of Three
题意同上题,不过是判断是否为3的幂,同理,使用相同套路。
代码如下:
class Solution {
public:
bool isPowerOfThree(int n) {
return !fmod(log10(n) / log10(3), 1);
}
};
相关文章推荐
- LeetCode - 231/326/342 - Power of Two/Three/Four
- LeetCode 231,326,342 --Power of Two & Three & Four
- [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four
- 231,338,326,Power of Two(Three,Four)Counting Bits 类似的技巧题
- LeetCode - 326, 342, 231 Power of Three, Four, and Two
- [leetcode][231] Power of Two
- [LeetCode 326] Power of Three
- LeetCode 231 Power of Two
- 【LeetCode-326】Power of three 下(C++)
- LeetCode 326 Power of Three
- LeetCode 326 Power of Three(3的幂)(递归、Log函数)
- [LeetCode 231] Power of Two
- Leetcode Power of Two/Three/Four
- LeetCode 231: Power of Two
- [Leetcode]#231 Power of Two
- (LeetCode 231)Power of Two
- leetcode_326_Power of Three(easy)(C++)
- Leetcode 231 Power of Two 数论
- 位操作-leetcode 231 Power of Two
- 【LeetCode-231】Power of Two