leet-code/src/main/java/leetcode/editor/cn/PowerOfThree.java
2021-09-23 09:06:59 +08:00

70 lines
1.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给定一个整数,写一个函数来判断它是否是 3 的幂次方。如果是,返回 true ;否则,返回 false 。
//
// 整数 n 是 3 的幂次方需满足:存在整数 x 使得 n == 3ˣ
//
//
//
// 示例 1
//
//
//输入n = 27
//输出true
//
//
// 示例 2
//
//
//输入n = 0
//输出false
//
//
// 示例 3
//
//
//输入n = 9
//输出true
//
//
// 示例 4
//
//
//输入n = 45
//输出false
//
//
//
//
// 提示:
//
//
// -2³¹ <= n <= 2³¹ - 1
//
//
//
//
// 进阶:
//
//
// 你能不使用循环或者递归来完成本题吗?
//
// Related Topics 递归 数学 👍 184 👎 0
package leetcode.editor.cn;
//326:3的幂
class PowerOfThree {
public static void main(String[] args) {
//测试代码
Solution solution = new PowerOfThree().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean isPowerOfThree(int n) {
return n > 0 && 1162261467 % n == 0;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}