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 的幂 #68

Closed
sailei1 opened this issue Jun 26, 2019 · 0 comments
Closed

3 的幂 #68

sailei1 opened this issue Jun 26, 2019 · 0 comments

Comments

@sailei1
Copy link
Owner

sailei1 commented Jun 26, 2019

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

示例 1:

输入: 27
输出: true
示例 2:

输入: 0
输出: false
示例 3:

输入: 9
输出: true
示例 4:

输入: 45
输出: false
进阶:
你能不使用循环或者递归来完成本题吗?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/power-of-three
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法:
不断除以3 确定最后的值, 然后判断是不是整除。

/**
 * @param {number} n
 * @return {boolean}
 */
var isPowerOfThree = function(n) {
   
    while(n>1){
        n=n/3;
    }
    return n===1;
};
@sailei1 sailei1 closed this as completed Jun 26, 2019
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