leetcode - 69.x square root

Beyond the time limit. .

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

Oh, good gas. . . Come to think about how to improve. . .

 

After modification by, but still very good, modify nearly 40 minutes, there is no good efficiency ah:

 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
When execution: 144 ms, beat the 5.80% of users in all Python3 submission
Memory consumption: 13.9 MB, beat the 5.22% of users in all Python3 submission
 
The others are doing well simple, I was too dull. . . . . .
                                                                                                                        ——2019.9.25

 

Guess you like

Origin www.cnblogs.com/taoyuxin/p/11583795.html