暴力
时间复杂度:
class Solution {
public:
/**
*
* @param x int整型
* @return int整型
*/
int mysqrt(int x) {
// write code here
long i = 0;
for(; i * i <= x; i ++);
return i * i > x ? i - 1: i;
}
}; 二分
时间复杂度:
class Solution {
public:
/**
*
* @param x int整型
* @return int整型
*/
int mysqrt(int x) {
// write code here
long left = 0, right = x;
while(left < right){
long mid = left + (right - left) / 2;
if(mid * mid >= x) right = mid;
else left = mid + 1;
}
return left * left > x ? left - 1 : left;
}
}; 
京公网安备 11010502036488号