LeetCode*326. Power of Three LeetCode题目链接 注意:凡是以英文出现的,都是题目提供的,包括答案代码里的前几行。 题目: 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? 解析: 这一题最开始的想......
res =log10(n)/log10(3);if(res- (int)res ==0)returntrue;elsereturnfalse; 三种解法的代码在 leetcode 网站的运行时间如下图: - 1、方法一 - 2、方法二 - 3、方法三 可见,第二种最好,第一种次之,第三种最差。 类似的题目还有 power of two, power of four,使用上述三种方法略加修改即可。但...
Power of Three -leetcode(java) Power of Three 题目 Given an integer n, return true if it is a power of three. Otherwise, return false. An integer n is a power of three, if there exists an integer x such that n == 3x. 思路 1.取对数 代码......
publicbooleanisPowerOfThree(intn){returnn >0&& (1162261467% n ==0); } It is worthwhile to mention that Method 1 works only when the base is prime. For example, we cannot use this algorithm to check if a number is a power of 4 or 6 or any other composite number. Method 2 Iflog...
bool isPowerOfThree(int n) { if(n<0){ return false; } return int(log10(n)/log10(3))-log10(n)/log10(3)==0; } }; 1. 2. 3. 4. 5. 6. 7. 8. 9. 参考文献 [LeetCode] Power of Three 判断3的次方数
这道题让我们判断一个数是不是3的次方数,在LeetCode中,有一道类似的题目Power of Two,那道题有个非常简单的方法,由于2的次方数实在太有特点,最高位为1,其他位均为0,所以特别容易,而3的次方数没有显著的特点,最直接的方法就是不停地除以3,看最后的余数是否为1,要注意考虑输入是负数和0的情况,参见代码如下...
2 年前· 来自专栏 LeetCode Tye关注CategoryDifficulty algorithms Easy (45.24%) Tags math Companies google Given an integer n, return true if it is a power of three. Otherwise, return false. 给定一个整数,写一个函数来判断它是否是 3 的幂次方。如果是,返回true;否则,返回false。 An integer n...
package leetcode // 解法一 数论 func isPowerOfThree(n int) bool { // 1162261467 is 3^19, 3^20 is bigger than int return n > 0 && (1162261467%n == 0) } // 解法二 打表法 func isPowerOfThree1(n int) bool { // 1162261467 is 3^19, 3^20 is bigger than int ...
leetcode.com/problems/power-of-three/ 2.1)采用循环 class Solution: def isPowerOfThree(self, n: int) -> bool: if not n: return False while n: if n==1: return True if n%3: return False n //=3 return True Runtime: 80 ms, faster than 52.08% of Python3 online submissions...
LeetCode 326. Power of Three 题目class Solution { public: bool isPowerOfThree(int n) { double x = log10(n) / log10(3); return n>0 && (abs((int)x - x) < 0.000000000000001); } }; ShenduCC 2020/07/13 2950 Leetcode 231. Power of Two https网络安全 版权声明:博客文章都是作者辛...