LeetCode 326. Power of Three
2017-09-03 09:43
453 查看
326. Power of Three
Description
Given an integer, write a function to determine if it is a power of three.Follow up: Could you do it without using any loop / recursion?
Solution
题意即判断一个数是不是3的次方。和之前的2、4次方不同,不能使用位运算来免除循环,但是我们发现,如果一个数是3的次方,那么在范围内最大的3的次方的数被这个数除,余数一定为0,而倘若不是3的次方,则余数不会为0,这个最大的数就是1162261467,在int范围内,可以通过写一个小循环快速找到。代码如下:
// C++/C class Solution { public: bool isPowerOfThree(int n) { return n > 0 && 1162261467 % n == 0; } };
# python class Solution(object): def isPowerOfThree(self, n): return n > 0 and 1162261467 % n == 0;
相关文章推荐
- leetcode(78).326. Power of Three
- 【Leetcode】:326. Power of Three 问题 in JAVA
- 【一天一道LeetCode】#326. Power of Three
- [LeetCode]326. Power of Three(判断整数是否是3的幂)
- leetcode 326. Power of Three
- leetcode_326. Power of Three-判断是否3的次方
- leetcode-java-326. Power of Three
- leetcode 326. Power of Three 3的幂指数 + 对数函数实现
- 【一天一道LeetCode】#326. Power of Three
- LeetCode ||326. Power of Three
- leetcode 326. Power of Three
- LeetCode 326. Power of Three
- 【LeetCode】 326. Power of Three
- LeetCode-326. Power of Three
- Leetcode——326. Power of Three
- leetcode_326. Power of Three 判断一个数是否是3的幂
- LeetCode 326. Power of Three
- leetcode 326. Power of Three
- 【leetcode】326. Power of Three
- Leetcode题解 326. Power of Three