[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:
Special thanks to @elmirap for adding this problem and creating all test cases.

这道题给了我们一个数,让我们判断其是否为完全平方数,那么显而易见的是,肯定不能使用brute force,这样太不高效了,那么最小是能以指数的速度来缩小范围,那么我最先想出的方法是这样的,比如一个数字49,我们先对其除以2,得到24,发现24的平方大于49,那么再对24除以2,得到12,发现12的平方还是大于49,再对12除以2,得到6,发现6的平方小于49,于是遍历6到12中的所有数,看有没有平方等于49的,有就返回true,没有就返回false,参见代码如下:

解法一:

class Solution {
public:
    bool isPerfectSquare(int num) {
        if (num == 1) return true;
        long x = num / 2, t = x * x;
        while (t > num) {
            x /= 2;
            t = x * x;
        }
        for (int i = x; i <= 2 * x; ++i) {
            if (i * i == num) return true;
        }
        return false;
    }
}; 

下面这种方法也比较高效,从1搜索到sqrt(num),看有没有平方正好等于num的数:

解法二:

class Solution {
public:
    bool isPerfectSquare(int num) {
        for (int i = 1; i <= num / i; ++i) {
            if (i * i == num) return true;
        }
        return false;
    }
}; 

我们也可以使用二分查找法来做,要查找的数为mid*mid,参见代码如下:

解法三:

class Solution {
public:
    bool isPerfectSquare(int num) {
        long left = 0, right = num;
        while (left <= right) {
            long mid = left + (right - left) / 2, t = mid * mid;
            if (t == num) return true;
            else if (t < num) left = mid + 1;
            else right = mid - 1;
        }
        return false;
    }
};

下面这种方法就是纯数学解法了,利用到了这样一条性质,完全平方数是一系列奇数之和,例如:

1 = 1
4 = 1 + 3
9 = 1 + 3 + 5
16 = 1 + 3 + 5 + 7
25 = 1 + 3 + 5 + 7 + 9
36 = 1 + 3 + 5 + 7 + 9 + 11
....
1+3+...+(2n-1) = (2n-1 + 1)n/2 = n*n

这里就不做证明了,我也不会证明,知道了这条性质,就可以利用其来解题了,时间复杂度为O(sqrt(n))。

解法四:

class Solution {
public:
    bool isPerfectSquare(int num) {
        int i = 1;
        while (num > 0) {
            num -= i;
            i += 2;
        }
        return num == 0;
    }
};

下面这种方法是第一种方法的类似方法,更加精简了,时间复杂度为O(lgn):

解法五:

class Solution {
public:
    bool isPerfectSquare(int num) {
        long x = num;
        while (x * x > num) {
            x = (x + num / x) / 2;
        }
        return x * x == num;
    }
};

本文转自博客园Grandyang的博客,原文链接:检验完全平方数[LeetCode] Valid Perfect Square ,如需转载请自行联系原博主。

相关文章
|
9月前
|
Go
golang力扣leetcode 279.完全平方数
golang力扣leetcode 279.完全平方数
58 0
|
9月前
|
Java
leetcode-279:完全平方数
leetcode-279:完全平方数
80 0
|
6月前
|
Python
【Leetcode刷题Python】279. 完全平方数
LeetCode 279题 "完全平方数" 的Python解决方案,使用动态规划来找到和为给定整数n的完全平方数的最少数量。
53 0
|
6月前
|
Python
【Leetcode刷题Python】367. 有效的完全平方数
本文提供了两种判断一个正整数是否为完全平方数的Python实现方法:一种是利用数学公式逐个减去奇数的方法,另一种是使用二分查找优化的暴力求解方法。
80 0
【超直白】leetcode 279 完全平方数
【超直白】leetcode 279 完全平方数
|
8月前
|
存储 SQL 算法
LeetCode 题目 65:有效数字(Valid Number)【python】
LeetCode 题目 65:有效数字(Valid Number)【python】
|
8月前
|
算法
【经典LeetCode算法题目专栏分类】【第6期】二分查找系列:x的平方根、有效完全平方数、搜索二位矩阵、寻找旋转排序数组最小值
【经典LeetCode算法题目专栏分类】【第6期】二分查找系列:x的平方根、有效完全平方数、搜索二位矩阵、寻找旋转排序数组最小值
|
9月前
[leetcode ~dp ]279. 完全平方数
[leetcode ~dp ]279. 完全平方数
|
9月前
代码随想录 Day38 完全背包问题 LeetCode T70 爬楼梯 T322 零钱兑换 T279 完全平方数
代码随想录 Day38 完全背包问题 LeetCode T70 爬楼梯 T322 零钱兑换 T279 完全平方数
49 0
|
9月前
leetcode279完全平方数刷题打卡
leetcode279完全平方数刷题打卡
50 0