首页 > 编程语言 > 详细

Python 解LeetCode:367. Valid Perfect Square

时间:2017-10-21 18:34:14      阅读:305      评论:0      收藏:0      [点我收藏+]

        题目描述:给出一个正整数,不使用内置函数,如sqrt(),判断这个数是不是一个数的平方。

       思路:直接使用二分法,貌似没啥好说的。代码如下:

 1 class Solution(object):
 2     def isPerfectSquare(self, num):
 3         """
 4         :type num: int
 5         :rtype: bool
 6         """
 7         left, right = 0, num
 8         while left <= right:
 9             mid = (left+right) / 2
10             if mid ** 2 == num:
11                 return True
12             elif mid ** 2 < num:
13                 left = mid + 1
14             else:
15                 right = mid -1
16         return False

 

Python 解LeetCode:367. Valid Perfect Square

原文:http://www.cnblogs.com/qiaojushuang/p/7705400.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!