Valid Perfect Square

Given a positive integer num, write a function which returns True if num is a perfect square else False.

Note: Do not use any built-in library function such as sqrt.

Example 1:

Input: 16
Returns: True

Example 2:

Input: 14
Returns: False
 1 public class Solution {
 2     public boolean isPerfectSquare(int num) {
 3         int low = 1, high = num;
 4
 5         while (low <= high) {
 6             int mid = low + (high - low) / 2;
 7
 8             if (mid == num / mid && num % mid == 0) return true;
 9             else if (mid > num / mid) high = mid - 1;
10             else low = mid + 1;
11         }
12         return false;
13     }
14 }
时间: 2024-11-10 15:34:47

Valid Perfect Square的相关文章

69. Sqrt(x) &amp;&amp; 367. Valid Perfect Square

69. Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. Hide Tags Binary Search Math Hide Similar Problems (M) Pow(x, n) (M) Valid Perfect Square Time Limit Exceed Solution if start from 0. public class Solution { public int m

LeetCode 第 367 题 (Valid Perfect Square)

LeetCode 第 367 题 (Valid Perfect Square) Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2

[leetcode] 367. Valid Perfect Square

Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False 使用二分查找寻找input

【leetcode】367. Valid Perfect Square]

题目描述: Given a positive integer num, write a function which returns True if num is a perfect square else False. 解题分析: 这种找数字的题一般都用类似与二分查找的算法.需要注意的是比较平方和时考虑到integer溢出的情况.所以这个结果是要用Long类型保存.由此到来的改变是判断相等时要用“equals()”方法,而不是“==”. 实现代码: 1 public class Solutio

367. Valid Perfect Square

题目: Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False 链接: https

[LeetCode] Valid Perfect Square 检验完全平方数

Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False Credits:Speci

[LeetCode] Valid Perfect Square

Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False Credits:Speci

367. Valid Perfect Square java solutions

Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2: Input: 14 Returns: False Credits:Speci

LintCode 777. Valid Perfect Square

Given a positive integer num, write a function which returns True if num is a perfect square else False. 样例 For example:Given num = 16Returns True 解法一:牛顿迭代器 class Solution { public: /** * @param num: a positive integer * @return: if num is a perfect