Leetcode 367. Valid Perfect Square

Source: Internet
Author: User

367. Valid Perfect Square
    • Total accepted:11219
    • Total submissions:30842
    • Difficulty:medium

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

Note:do no built-in library function such as sqrt .

Example 1:

 - returns:true

Example 2:

 - Returns:false

Train of thought: Dichotomy method.

Code:

1 classSolution {2  Public:3     BOOLIsperfectsquare (intnum) {4         intleft=1, right=num-1;5          while(left<=Right ) {6             Long Longmid=left+ (Right-left)/2;7             if(mid*mid<num) {8Left=mid+1;9             }Ten             Else{ Oneright=mid-1; A             } -         } -         returnleft*left==num; the     } -};

Leetcode 367. Valid Perfect Square

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.