Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3的幂 #29

Open
Aras-ax opened this issue Apr 4, 2019 · 0 comments
Open

3的幂 #29

Aras-ax opened this issue Apr 4, 2019 · 0 comments

Comments

@Aras-ax
Copy link
Owner

Aras-ax commented Apr 4, 2019

给定一个整数,编写一个函数来判断它是否是 3 的幂次方。

示例 1:

输入: 1
输出: true
解释: 3^0 = 1

示例 2:

输入: 27
输出: true
解释: 3^3 = 27

示例 3:

输入: 218
输出: false

解答

function isPowerOfThree(num) {
    if (num >= 1) {
        return num === 1 || (num % 3 === 0 && isPowerOfThree(num / 3));
    }
    return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant