实现函数 int mysqrt(int x).
计算并返回 x 的平方根(向下取整)
数据范围:
要求:空间复杂度 ,时间复杂度
import java.util.*; public class Solution { /** * * @param x int整型 * @return int整型 */ public int mysqrt (int x) { // write code here //return (int) Math.mysqrt(x); for(int i = 0; i <= x; i++){ if(i * i > x){ return i - 1; } if(i * i == x){ return i; } } return 0; } }
import java.util.*; public class Solution { /** * * @param x int整型 * @return int整型 */ public int mysqrt (int x) { int l = 0, r = x; while(l <= r){ int mid = (l+r) >> 1; long tem = (long)mid * mid; if(tem > x){ r = mid - 1; }else if(tem < x){ l = mid + 1; }else{ return mid; } } return r; } }
# # # @param x int整型 # @return int整型 # class Solution: def mysqrt(self , x ): # write code here # 二分查找 # if x==1 or x==0: # return x # high = x # low = 0 # mid = (high+low)/2 # while abs(mid*mid - x) > 0.01: # if mid*mid>x: # high,mid = mid,(high+low)/2 # else: # low,mid = mid,(high+low)/2 # return int(mid) # 牛顿法 if x==1 or x==0: return x sqr = x/2 while abs(sqr*sqr - x)>0.01: sqr = (sqr+x/sqr)/2 return int(sqr)
class Solution: def mysqrt(self , x ): # write code here if x==0: return 0 C,x0=x,x while True : xi=0.5*(x0+C/x0) if abs(x0-xi)<1e-7 : break x0=xi return int(x0)
class Solution: def equal(self,low,high,m): if low>=high: return low mid=(high-low)//2+low if mid**2==m: return mid elif mid**2<m: return self.equal(mid + 1, high, m) else: return self.equal(low, mid - 1, m) def mysqrt(self , x ): # write code here if x<=0: return 0 low=1 high=x a=self.equal(low,high,x) if a**2>x: a-=1 return a
可采用二分法,但是需要注意溢出问题。
class Solution { public: /** * * @param x int整型 * @return int整型 */ int mysqrt(int x) { int l = 0, r = x; while (l < r) { int mid = l + ((r - l) >> 1); long long tmp = 1LL * mid * mid; // 注意溢出,因此采用 long long if (tmp < x) { if ((mid + 1) * (mid + 1) > x) { return mid; } l = mid + 1; } else { r = mid; } } return l; } };
public class Solution {
public int mysqrt(int x) {
return (int) Math.mysqrt(x);
}
}
public class Solution {
public int mysqrt(int x) {
int i = 1;
int res = 0;
while (x >= 0) {
x -= i;
res++;
i += 2;
}
return res - 1;
}
}
class Solution { public: int mysqrt(int x) {//思路用二分法 if (x < 2) return x; int left = 1, right = x / 2; //右端从x/2开始 int mid, last_mid; while (left <= right) { mid = left + (right - left) / 2; if (x / mid > mid) { //不用x > mid * mid 会溢出 left = mid + 1; last_mid = mid; } else if (x / mid < mid) right = mid - 1; else return mid; } return last_mid; } };
public class Solution { //牛顿法思路: //r = mysqrt(x),即r*r = x //故有f(r) = r*r-x = 0 这里注意r是变量,x是常数 //按照牛顿迭代法公式 r_new = r_old + f(r)/f'(r) //得 r_new = (r_old+x/r_old)/2 //一般,牛顿法是当 一次迭代的变化量足够小时就停止迭代 //这里利用了f(r)的特征,将r初始化为x,可以画图查看,r的初始值在mysqrt(x)的右半边 //在循环内的迭代过程中,r始终在mysqrt(x)的右半边。直到退出循环得出答案 public int mysqrt(int x) { if(x==0 || x==1) return x; long r = x; while(x/r<r)//即r*r>x r = (r+x/r)/2; //此时r*r<=x 即为答案 return (int)r; } }
// public static int mysqrt(int x) { // return (int)Math.mysqrt(x); // } 但是题的意思不是用这个 特比特别要注意两点:第一right要取x/2+1 这个还不是最重要的,其实只是影响速度 第二:要用x/middle>middle 来表示x>middle*middle 不然会溢出 第三:判断相等时用x/middle>=middle && x/(middle+1)<(middle+1) public static int mysqrt(int x) { if(x==0){ return 0; } if(x<0){ return -1; } int left = 1,right = x/2+1,middle ; while(left<=right){ middle = (left+right)/2; if(x/middle>=middle && x/(middle+1)<(middle+1)){ return middle; }else if(x/middle>middle){ left = middle+1; }else{ right = middle-1; } } return right; }
public class Solution { public int mysqrt(int x) { if (x== 0) return 0; int left = 1, right = x; while (true) { int mid = left + (right - left) / 2; //这里判断不用if (mid * mid > x),因为使用mid > x / mid一定会有结果 if (mid > x / mid) right = mid - 1; else { if(mid+1>x/(mid+1)) return mid; left=mid+1; } } } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return int整型 * * C语言声明定义全局变量请加上static,防止重复定义 * * C语言声明定义全局变量请加上static,防止重复定义 */ int Sqrt(int x ) { // write code her return mysqrt(x)/1; }就是懒~~~ 佬勿喷
class Solution { public: /** * * @param x int整型 * @return int整型 */ int mysqrt(int x) { // write code here double l = 1, r = x, mid; while (r - l >= 0.2) { mid = (l + r) / 2; if (mid * mid > x) r = mid; if (mid * mid == x) return int(mid); if (mid * mid < x) l = mid; } return int(r); } };
/* ** 牛顿法 5s */ /* class Solution { public: int mysqrt(int x) { if(x < 0) return -1; long res = x; while(res * res > x) res = ((res + x / res) >> 1); return res; } }; /* ** 二分法 4s */ class Solution { public: int mysqrt(int x) { if(x < 0) return -1; long low = 0; long high = x; long mid = 0; while(low <= high) { mid = low + ((high - low) >> 1); if(mid * mid == x || (mid * mid < x && (mid + 1) * (mid + 1) > x)) return mid; else if(mid * mid > x) high = mid - 1; else low = mid + 1; } return 0; } };