res =log10(n)/log10(3);if(res- (int)res ==0)returntrue;elsereturnfalse;#endif}intmain(){intnum =4782968;boolres =false; res = isPowerOfThree(num);printf("res = %d\n",res);return0; }
[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。 ......
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...
Description Given an integer, write a function to determine if it is a power of three. Example 1: Input: 27 1. Output: true 1. Example 2: Input: 0 1. Output: false 1. Example 3: Input: 9 1. Output: true 1. Example 4: Input: 45 1. Output: false 1. Follow up: Could you ...
classSolution {public:boolisPowerOfThree(intn) {return(n >0&&1162261467% n ==0); }}; 最后还有一种巧妙的方法,利用对数的换底公式来做,高中学过的换底公式为logab = logcb / logca,那么如果n是3的倍数,则log3n一定是整数,我们利用换底公式可以写为log3n = log10n / log103,注意这里一定要用10...
[leetcode]326. Power of Three problem 326. Power of Three 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....
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...
326. 3 的幂 - 给定一个整数,写一个函数来判断它是否是 3 的幂次方。如果是,返回 true ;否则,返回 false 。 整数 n 是 3 的幂次方需满足:存在整数 x 使得 n == 3x 示例 1: 输入:n = 27 输出:true 示例 2: 输入:n = 0 输出:false 示例 3: 输入:n
A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two. For example: "112358" is an additive number because the digits can form an additive sequence: 1, 1, 2,...
LeetCode笔记:342. Power of Four 问题: 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?