[Leetcode] Valid Perfect Square test Complete square number

Source: Internet
Author: User

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:16returns:true

Example 2:

Input:14returns:false

Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.

This problem gives us a number, let us determine whether it is a complete square number, then it is obvious that we must not use brute force, so it is not efficient, so the smallest can be exponential speed to narrow the range, then I first think of the method is this, such as a number 49, we first divided it by 2 , get 24, found that 24 squared is greater than 49, then 24 divided by 2, get 12, found that 12 squared or greater than 49, then 12 divided by 2, get 6, found that 6 squared is less than 49, and then traverse 6 to 12 of all the number, see if there is no square equals 49, there is return true, return FALSE if not, see the code below:

Solution One:

classSolution { Public:    BOOLIsperfectsquare (intnum) {        if(num = =1)return true; Longx = num/2, t = x *x;  while(T >num) {x/=2; T= x *x; }         for(inti = x; I <=2* x; ++i) {if(i * = = num)return true; }        return false; }}; 

The following method is also more efficient, from 1 to sqrt (num), see if there is no square equal to num number:

Solution Two:

class Solution {public:    bool isperfectsquare (int  num) {          for (int1; I <= num/i; + +i)            {ifreturntrue ;        }         return false ;    

We can also use the binary lookup method, to find the number of Mid*mid, see the code as follows:

Solution Three:

classSolution { Public:    BOOLIsperfectsquare (intnum) {        Longleft =0, right =num;  while(Left <=Right ) {            LongMid = left + (right-left)/2, T = Mid *mid; if(t = num)return true; Else if(T < num) left = mid +1; Elseright = mid-1; }        return false; }};

The following method is pure mathematical solution, the use of such a property, the total square number is a series of odd sum, for example:

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

There is no proof, I will not prove that, knowing the nature, you can use it to solve the problem, the time complexity of O (sqrt (n)).

Solution Four:

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

The following method is a similar approach to the first method, which is more streamlined and has a time complexity of O (LGN):

Solution Five:

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

This problem actually has the O (1) solution, this you believe? That's insane, please see this post on the forum for details.

Similar topics:

SQRT (x)

Resources:

Https://leetcode.com/discuss/110639/o-logn-bisection-method

Https://leetcode.com/discuss/110792/simple-for-loop-o-sqrt-n

Https://leetcode.com/discuss/110638/a-square-number-is-1-3-5-7-java-code

Https://leetcode.com/discuss/110659/o-1-time-c-solution-inspired-by-q_rsqrt

Https://leetcode.com/discuss/110671/3-4-short-lines-integer-newton-most-languages

Leetcode all in one topic summary (continuous update ...)

[Leetcode] Valid Perfect Square test Complete square number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.