LeetCode - 326. Power of Three
题目
LeetCode - 326. Power of Three
题目链接
https://leetcode.com/problems/power-of-three/
参考博客
解题思路
解法二一开始打算使用$3^{log_3N}== N$,来判断是否是3的幂,利用的是取整后去尾,再还原。但是pow运算,和log运算时浮点运算,会有精度丢失。C语言中log是以e为底的对数,计算时精度丢失严重,建议用以10为底的对数log10.
解题源码
- 解法一
12345678910 | class {public: bool isPowerOfThree(int大专栏 LeetCode - 326. Power of Threepan> n) { long long int a = 1; while(a < n){ a *= 3; } return a == n; }}; |
- 解法二
123456 | class {public: bool isPowerOfThree(int n) { return (n > 0 && log10(n)/log10(3) == int(log10(n)/log10(3))); }}; |
相关推荐
aanndd 2020-08-12
aanndd 2020-07-26
aanndd 2020-07-08
zangdaiyang 2020-07-04
yaohustiAC 2020-06-28
us0 2020-06-28
yaohustiAC 2020-06-28
zangdaiyang 2020-06-28
Clairezz 2020-06-28
嗡汤圆 2020-06-26
嗡汤圆 2020-06-21
aanndd 2020-06-16
aanndd 2020-06-16
码墨 2020-06-16
yaohustiAC 2020-06-11
zangdaiyang 2020-06-10
jiayuqicz 2020-06-09
yaohustiAC 2020-06-06
heray0 2020-06-04