本文主要是介绍leetcode367. 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
思路:二分法
class Solution(object):def isPerfectSquare(self, num):""":type num: int:rtype: bool"""start=0end=(num+1)/2+1while(end-start>1):mid=start+(end-start)/2tmp=mid*midif tmp<num:start=midelif tmp>num:end=midelse:return Truereturn False
这篇关于leetcode367. Valid Perfect Square的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!