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:

Input: 14

Returns: False

这道题可以借用第 69 题的代码。第 69 题中我们实现了自己的 Sqrt 函数,既然这道题不让用库函数中的 sqrt,那么我们就自己写个,用简单的二分查找法就可以。代码的具体解释见第 69 题的解答。

下面是代码。

int mySqrt(int x)
{
    if(x <= 0) return 0;
    int a1 = 1;
    int a2 = 46341 * 2 - 1;
    unsigned int a, y;
    if(a2 > x / 2) a2 = x;
    do
    {
        a = (a1 + a2) / 2;
        y = a * a;
        if(y == x) return a;
        if(y > x)
        {
            a2 = a;
        }
        else
        {
            a1 = a;
        }
    }while(a1 + 1 < a2);
    a = (a1 + a2) / 2;
    return a;
}
bool isPerfectSquare(int num)
{
    int z = mySqrt(num);
    return z * z == num;
}
时间: 2024-11-05 20:37:53

LeetCode 第 367 题 (Valid Perfect Square)的相关文章

LeetCode算法题-Valid Perfect Square(Java实现-四种解法)

这是悦乐书的第209次更新,第221篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第77题(顺位题号是367).给定正整数num,写一个函数,如果num是一个完美的正方形,则返回True,否则返回False.例如: 输入:16 输出:true 输入:14 输出:false 注意:不要使用任何内置库函数,例如sqrt. 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试. 02 第一种解法 暴力解法

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]

题目描述: 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

[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

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

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

[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

[LC] 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 Output: true Example 2: Input: 14 Output: false class Solution