69. Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x.

题目题目:返回一个int数开方后的结果

 1     public int mySqrt(int x) {
 2         if (x == 0)
 3             return 0;
 4         int left = 1, right = x;
 5         int ans = 1;
 6         while (left<=right)
 7         {
 8             int mid = left+ (right-left)/2;
 9             if (mid == x/mid)
10             {
11 
12                 return mid;
13             }
14             else if (mid>x/mid) right=mid-1;
15             else left=mid+1;
16         }
17         return right;     
18     }

 

posted @ 2017-10-20 19:40  daniel456  阅读(109)  评论(0编辑  收藏  举报