博主并没有什么算法基础,所以写的不好,勿喷,抛砖引玉,欢迎交流,感谢。
//给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方。
// 示例 1:
// 输入: 16
//输出: true
// 示例 2:
// 输入: 5
//输出: false
// 进阶:
//你能不使用循环或者递归来完成本题吗?
// Related Topics 位运算
// 👍 136 👎 0
package com.zqh.leetcode.editor.cn;
//Java:4的幂
public class P342PowerOfFour {
public static void main(String[] args) {
Solution solution = new P342PowerOfFour().new Solution();
System.out.println(solution.isPowerOfFour(4));
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public boolean isPowerOfFour(int num) {
return method(num);
}
//暴力破解
public boolean method(int n) {
if (n == 0) {
return false;
}
while (true) {
if (n == 1) {
return true;
}
if (n % 4 != 0) {
return false;
}
n = n / 4;
}
}
}
//leetcode submit region end(Prohibit modification and deletion)
}