[LeetCode] 326. Power of Three + 342. Power of Four

这两题我放在一起说是因为思路一模一样,没什么值得研究的。思路都是用对数的换底公式去判断。

 Reference, https://blog.csdn.net/qy20115549/article/details/52849280

JavaScript实现

1 /**
2  * @param {number} n
3  * @return {boolean}
4  */
5 var isPowerOfThree = function(n) {
6     return (Math.log10(n) / Math.log10(3)) % 1 === 0;
7 };

 

1 /**
2  * @param {number} num
3  * @return {boolean}
4  */
5 var isPowerOfFour = function(num) {
6     return (Math.log(num) / Math.log(4)) % 1 == 0;
7 };

 

Java实现

1 class Solution {
2     public boolean isPowerOfThree(int n) {
3         return (Math.log10(n) / Math.log10(3)) % 1 == 0;
4     }
5 }

 

1 class Solution {
2     public boolean isPowerOfFour(int num) {
3         return Math.log(num) / Math.log(4) % 1 == 0;
4     }
5 }

 

LeetCode 题目总结

posted @ 2020-03-25 13:20  CNoodle  阅读(151)  评论(0编辑  收藏  举报