今天继续刷LeetCode,第326题,判断一个数是否是3的阶乘。 分析: 方法一:递归,不断对n进行除3操作,判断结果是否为1; 方法二:循环, 问题: 1、递归需要利用栈; 附上C++代码: class Solution { public: bool isPowerOfThree(int n) { if(n<=0) return false; if(n==1) return true; if(n%3==0) ...
*326. Power of Three * three ways to solution this problem */#include<stdio.h>#include<stdbool.h>#include<math.h>#definesolution 3boolisPowerOfThree(intn){#ifsolution==1//循环迭代while(n) {if(n==1)returntrue;if(n%3!=0)returnfalse; n /=3;if(n ==1)returntrue; }returnfalse;...
publicbooleanisPowerOfThree6(intn){if(n <=1) {returnn==1; }returnInteger.toString(n,3).matches("10*"); } 08 第七种解法 将int类型范围内所有3的幂次方整数放入一个HashSet中,然后判断n是否在HashSet中。 publicbooleanisPowerOfThree7(intn){ HashSet<Integer> set =newHashSet<>(Arrays.asLis...
[LeetCode] Power of Two Given an integer, write a function to determine if it is a power of two. 这道题没啥难度。主要是搞清楚怎么来判断一个数是不是power of two。 最简单的方法就是一直除以2最后等于1。如果不能,那么都是false。 ......
classSolution {public:boolisPowerOfThree(intn) {return(n >0&∫(log10(n) / log10(3)) - log10(n) / log10(3) ==0); }}; 类似题目: Power of Two 参考资料: https://leetcode.com/discuss/78532/summary-all-solutions-new-method-included-at-15-30pm-jan-8th...
class Solution { public: 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的次方数...
solution: Iteration class Solution { public: bool isPowerOfThree(int n) { if(n<1) return false;//err. while(n%3==0) { n /= 3; } return n==1;//err. } }; 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. ref 1. Leetcode_326_Power of Three; ...
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...
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网络安全 版权声明:博客文章都是作者辛苦整理的,转载请...
2)Power of 3. 是否为3的幂指数 Loading...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 ...