每天Leetcode 刷题 初级算法篇-数学问题-3的幂

题目要求:

在这里插入图片描述

力扣题解:循环迭代

在这里插入图片描述

代码

/**
 * @program: mydemo
 * @description: 判断一个数是不是3的幂
 * @author: Mr.zeng
 * @create: 2021-02-23 09:45
 **/
public class Solution34 {
    
    
    public boolean isPowerOfThree(int n) {
    
    
        if (n < 1) {
    
    
            return false;
        }
        while (n % 3 == 0) {
    
    
            n /= 3;
        }
        return n == 1;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42292697/article/details/113973517