您的位置:首页 > 编程语言 > C语言/C++

Leetcode 326. Power of Three (Easy) (cpp)

2016-07-14 10:52 465 查看
Leetcode 326. Power of Three (Easy) (cpp)

Tag: Math

Difficulty: Easy

/*
326. Power of Three (Easy)

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?

*/
class Solution {
public:
bool isPowerOfThree(int n) {
if (n <= 0)
return false;
if(n == 1)
return true;
if(n % 3 == 0)
return isPowerOfThree(n / 3);
return false;
}
};
class Solution {
public:
bool isPowerOfThree(int n) {
if(n > 0)
return 1162261467 % n == 0;
else
return false;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: