标签:
Implement int sqrt(int x)
.
1 class Solution 2 { 3 public: 4 int sqrt(int n) 5 { 6 int m = n; 7 if(n == 0 || n == 1) 8 return n; 9 10 int s = 0, e = n, mid = 0; 11 if(e > 46341) 12 e = 46341; 13 14 while(s < e) 15 { 16 mid = (s + e)/ 2; 17 if(mid * mid < n) 18 s = mid; 19 else if(mid * mid > n) 20 e = mid; 21 else 22 return mid; 23 24 if(s + 1 == e) 25 return s; 26 } 27 } 28 };
标签:
原文地址:http://www.cnblogs.com/lxd2502/p/4249994.html