本文主要是介绍【leetcode】69. Sqrt(x)【M】【74】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Implement int sqrt(int x)
.
Compute and return the square root of x.
Subscribe to see which companies asked this question
二分法,最后再加一个处理
这么简单的题目竟然才做。。sigh
class Solution(object):def mySqrt(self, x):start = 1end = xwhile start < end:#print start,mid,endmid = (start + end) / 2res = mid ** 2if res > x:end = mid - 1elif res == x:return midelse:start = mid + 1if start ** 2 > x:start -= 1return start
这篇关于【leetcode】69. Sqrt(x)【M】【74】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!