Created
March 20, 2019 23:47
-
-
Save qiaoxu123/222e9b58b1c93966c07bd4299155dcb8 to your computer and use it in GitHub Desktop.
> 求3的n次方。题目实现的最简单方式是使用循环或者递归来实现,从而判断这个数值是否是3的n次方。 但题目本身提出,能够不使用循环或者递归,在这使用数学log来实现
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 52 ms, faster than 96.57% | |
//Memory Usage: 8.5 MB, less than 5.67% | |
class Solution { | |
public: | |
bool isPowerOfThree(int n) { | |
return fmod(log10(n) / log10(3),1) == 0; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Runtime: 64 ms, faster than 24.90% | |
//Memory Usage: 8.2 MB, less than 34.82% | |
class Solution { | |
public: | |
bool isPowerOfThree(int n) { | |
if(n <= 0) return false; | |
long int num = 1; | |
while(1){ | |
if(num < n) | |
num *= 3; | |
else if(num == n) | |
return true; | |
else | |
return false; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment