Python solution leetcode:367. Valid Perfect Square

Source: Internet
Author: User

Title Description: give a positive integer, do not use built-in functions, such as sqrt (), to determine whether this number is a number of squares.

Idea: Direct use of dichotomy, seemingly nothing to say. The code is as follows:

1 classsolution (object):2     defisperfectsquare (self, num):3         """4 : Type Num:int5 : Rtype:bool6         """7Left, right =0, Num8          whileLeft <=Right :9Mid = (left+right)/2Ten             ifMid * * 2 = =Num: One                 returnTrue A             elifMid * * 2 <Num: -left = mid + 1 -             Else: theright = Mid-1 -         returnFalse

Python solution leetcode:367. Valid Perfect Square

Related Article

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: 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.