Implementint sqrt(int x).Compute and return the square root of x.
//给定数字x 求解x的开方。
class Solution { public: int sqrt(int x) { if(x <=1) return x; int begin = 1; int end = x; int middle = 0; while(begin <= end){ middle = begin+(end-begin)/2; if(middle ==x/middle) return middle; else{ if(middle < x/middle){ begin = middle+1; }else{ end = middle-1; } } } if(end*end>x) return end-1; return end; } };
时间: 2024-10-12 04:17:13