326:3的幂
This commit is contained in:
parent
4442d04a34
commit
0ef5e4830a
70
src/main/java/leetcode/editor/cn/PowerOfThree.java
Normal file
70
src/main/java/leetcode/editor/cn/PowerOfThree.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
//给定一个整数,写一个函数来判断它是否是 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)
|
||||||
|
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
50
src/main/java/leetcode/editor/cn/doc/content/PowerOfThree.md
Normal file
50
src/main/java/leetcode/editor/cn/doc/content/PowerOfThree.md
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<p>给定一个整数,写一个函数来判断它是否是 3 的幂次方。如果是,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
|
||||||
|
|
||||||
|
<p>整数 <code>n</code> 是 3 的幂次方需满足:存在整数 <code>x</code> 使得 <code>n == 3<sup>x</sup></code></p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 27
|
||||||
|
<strong>输出:</strong>true
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 0
|
||||||
|
<strong>输出:</strong>false
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 9
|
||||||
|
<strong>输出:</strong>true
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 4:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>n = 45
|
||||||
|
<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>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>你能不使用循环或者递归来完成本题吗?</li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>递归</li><li>数学</li></div></div><br><div><li>👍 184</li><li>👎 0</li></div>
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user