342:4的幂

This commit is contained in:
huangge1199 2021-05-31 09:34:34 +08:00
parent 6e2eca177b
commit 8c0f60e01d
4 changed files with 109 additions and 5 deletions

View File

@ -0,0 +1,64 @@
//给定一个整数写一个函数来判断它是否是 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)
}

View File

@ -0,0 +1,43 @@
<p>给定一个整数,写一个函数来判断它是否是 4 的幂次方。如果是,返回 <code>true</code> ;否则,返回 <code>false</code></p>
<p>整数 <code>n</code> 是 4 的幂次方需满足:存在整数 <code>x</code> 使得 <code>n == 4<sup>x</sup></code></p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>n = 16
<strong>输出:</strong>true
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>n = 5
<strong>输出:</strong>false
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>n = 1
<strong>输出:</strong>true
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>-2<sup>31</sup> <= n <= 2<sup>31</sup> - 1</code></li>
</ul>
<p> </p>
<p><strong>进阶:</strong></p>
<ul>
<li>你能不使用循环或者递归来完成本题吗?</li>
</ul>
<div><div>Related Topics</div><div><li>位运算</li></div></div>\n<div><li>👍 199</li><li>👎 0</li></div>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long