367:有效的完全平方数
This commit is contained in:
parent
2f48a2d12f
commit
177834ce5e
66
src/main/java/leetcode/editor/cn/ValidPerfectSquare.java
Normal file
66
src/main/java/leetcode/editor/cn/ValidPerfectSquare.java
Normal file
@ -0,0 +1,66 @@
|
||||
//给定一个 正整数 num ,编写一个函数,如果 num 是一个完全平方数,则返回 true ,否则返回 false 。
|
||||
//
|
||||
// 进阶:不要 使用任何内置的库函数,如 sqrt 。
|
||||
//
|
||||
//
|
||||
//
|
||||
// 示例 1:
|
||||
//
|
||||
//
|
||||
//输入:num = 16
|
||||
//输出:true
|
||||
//
|
||||
//
|
||||
// 示例 2:
|
||||
//
|
||||
//
|
||||
//输入:num = 14
|
||||
//输出:false
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// 提示:
|
||||
//
|
||||
//
|
||||
// 1 <= num <= 2^31 - 1
|
||||
//
|
||||
// Related Topics 数学 二分查找 👍 371 👎 0
|
||||
|
||||
package leetcode.editor.cn;
|
||||
|
||||
//367:有效的完全平方数
|
||||
public class ValidPerfectSquare {
|
||||
public static void main(String[] args) {
|
||||
Solution solution = new ValidPerfectSquare().new Solution();
|
||||
// TO TEST
|
||||
System.out.println(solution.isPerfectSquare(16));
|
||||
System.out.println(solution.isPerfectSquare(808201));
|
||||
}
|
||||
|
||||
//leetcode submit region begin(Prohibit modification and deletion)
|
||||
class Solution {
|
||||
public boolean isPerfectSquare(int num) {
|
||||
if (num * num == num) {
|
||||
return true;
|
||||
}
|
||||
int left = 0;
|
||||
int right = num;
|
||||
while (left < right) {
|
||||
int mid = (right - left) / 2 + left;
|
||||
long mul = (long) mid * mid;
|
||||
if (mul == num) {
|
||||
return true;
|
||||
}
|
||||
if (mul < num) {
|
||||
left = mid + 1;
|
||||
} else {
|
||||
right = mid;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//leetcode submit region end(Prohibit modification and deletion)
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
<p>给定一个 <strong>正整数</strong> <code>num</code> ,编写一个函数,如果 <code>num</code> 是一个完全平方数,则返回 <code>true</code> ,否则返回 <code>false</code> 。</p>
|
||||
|
||||
<p><strong>进阶:不要</strong> 使用任何内置的库函数,如 <code>sqrt</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 16
|
||||
<strong>输出:</strong>true
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>num = 14
|
||||
<strong>输出:</strong>false
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= num <= 2^31 - 1</code></li>
|
||||
</ul>
|
||||
<div><div>Related Topics</div><div><li>数学</li><li>二分查找</li></div></div><br><div><li>👍 371</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user