leet-code/src/main/java/leetcode/editor/cn/PowerOfFour.java
2021-05-31 09:34:34 +08:00

64 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.

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