LeetCode 326: Power of Three
今天突然发现做LeetCode上Math的题目真心很有趣,不仅解法多样,还能时不时被绝妙的想法高潮一波。
Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
思路
这道题目因为有int的限制,所以知道3的次幂的数肯定大不过1162261467。
代码
|
|
利用log实现
|
|
题目说明了不可以使用循环或递归:
|
|
|
|
21038 / 21038 test cases passed.
Status: Accepted
Runtime: 120 ms