231:2 的幂
This commit is contained in:
parent
5062a8a76f
commit
1fc960edf9
74
src/main/java/leetcode/editor/cn/PowerOfTwo.java
Normal file
74
src/main/java/leetcode/editor/cn/PowerOfTwo.java
Normal file
@ -0,0 +1,74 @@
|
||||
//给你一个整数 n,请你判断该整数是否是 2 的幂次方。如果是,返回 true ;否则,返回 false 。
|
||||
//
|
||||
// 如果存在一个整数 x 使得 n == 2x ,则认为 n 是 2 的幂次方。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:n = 1
|
||||
//输出:true
|
||||
//解释:20 = 1
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:n = 16
|
||||
//输出:true
|
||||
//解释:24 = 16
|
||||
//
|
||||
//
|
||||
// 示例 3:
|
||||
//
|
||||
//
|
||||
//输入:n = 3
|
||||
//输出:false
|
||||
//
|
||||
//
|
||||
// 示例 4:
|
||||
//
|
||||
//
|
||||
//输入:n = 4
|
||||
//输出:true
|
||||
//
|
||||
//
|
||||
// 示例 5:
|
||||
//
|
||||
//
|
||||
//输入:n = 5
|
||||
//输出:false
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// -231 <= n <= 231 - 1
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 进阶:你能够不使用循环/递归解决此问题吗?
|
||||
// Related Topics 位运算 数学
|
||||
// 👍 362 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
//231:2 的幂
|
||||
class PowerOfTwo{
|
||||
public static void main(String[] args) {
|
||||
//测试代码
|
||||
Solution solution = new PowerOfTwo().new Solution();
|
||||
}
|
||||
//力扣代码
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public boolean isPowerOfTwo(int n) {
|
||||
return n > 0 && Integer.bitCount(n) == 1;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
55
src/main/java/leetcode/editor/cn/PowerOfTwo.md
Normal file
55
src/main/java/leetcode/editor/cn/PowerOfTwo.md
Normal file
@ -0,0 +1,55 @@
|
||||
<p>给你一个整数 <code>n</code>,请你判断该整数是否是 2 的幂次方。如果是,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||
|
||||
<p>如果存在一个整数 <code>x</code> 使得 <code>n == 2<sup>x</sup></code> ,则认为 <code>n</code> 是 2 的幂次方。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 1
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>2<sup>0</sup> = 1
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 16
|
||||
<strong>输出:</strong>true
|
||||
<strong>解释:</strong>2<sup>4</sup> = 16
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 3
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 4
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 5
|
||||
<strong>输出:</strong>false
|
||||
</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>
|
||||
<div><div>Related Topics</div><div><li>位运算</li><li>数学</li></div></div>\n<div><li>👍 362</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
Loading…
Reference in New Issue
Block a user