描述
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?
分析
我记得以前有写过这样的题目,所以就随手写出来了,,,C语言的版本是评论区里见到的,简直是神代码,原理是这样的:$2^31 - 1= 1162261467 $,而$1162261467=3^19$,所以在c中,能被1162261467整除的,一定是3的某次幂
解决方案1(C)
1 | bool isPowerOfThree(int n) { |
解决方案2(C++)
1 | class Solution { |
解决方案3(Python)
1 | class Solution(object): |