leetcode——69.x的平方根

时间:2019-09-25
本文章向大家介绍leetcode——69.x的平方根,主要包括leetcode——69.x的平方根使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

超出时间限制。。

1 class Solution:
2     def mySqrt(self, x: int) -> int:
3         for i in range(0,x//2+2):
4             if x>=i**2 and x<(i+1)**2:
5                 return i

好气哦。。。加油想想怎么改进。。。

修改以后通过,但是还是太好,修改了将近40分钟,好没有效率啊:

 1 class Solution:
 2     def mySqrt(self, x: int) -> int:
 3         i=x
 4         m=[x]
 5         while i>=0:
 6             if i**2>x:
 7                 m[0]=i
 8                 i=i//2
 9             elif x>=i**2 and x<(i+1)**2:
10                 return i
11             else:
12                 i=(i+m[0])//2
执行用时 :144 ms, 在所有 Python3 提交中击败了5.80%的用户
内存消耗 :13.9 MB, 在所有 Python3 提交中击败了5.22%的用户
 
别人的做法好简单,我就是太愚钝。。。。。。
                                                                                                                        ——2019.9.25

原文地址:https://www.cnblogs.com/taoyuxin/p/11583795.html