LeetCode-326

Links:https://leetcode.com/problems/power-of-three/

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?

思路:

1.用2^31次方内,最大的3的幂去除n取余…判断余数是否为0

2.用log()函数.判断n是3的多少次方(是不是一个整数),用log10()是因为精度问题.

代码1:

代码2:

 

【LeetCode】326. Power of Three
Tagged on:
0 0 vote
Article Rating
订阅
提醒
0 评论
Inline Feedbacks
View all comments