实现函数 int mysqrt(int x).
计算并返回 x 的平方根(向下取整)
数据范围:
要求:空间复杂度 ,时间复杂度
public int mysqrt (int x) { // write code here long p1=0 ,p2=x; while(p1<=p2){ long mid=(p1+p2)/2; if(mid*mid<=x && (mid+1)*(mid+1)>x){ return (int)mid; }else if(x<(mid*mid)){ p2=mid-1; }else{ p1=mid+1; } } return -1; }
import java.util.*; public class Solution { /** * * @param x int整型 * @return int整型 */ public int mysqrt (int x) { // write code here if (x == 0 || x == 1) { return x; } int left = 2; int right = x; while (true) { int mid = left + ((right - left) >> 1); // 这里不用mid * mid > x,防止mid *mid 的结果太大,溢出 if (mid > x/mid) { right = mid - 1; } else { // mid^2<=x (mid+1)^2>x 所以mid就是那个值 if(mid+1>x/(mid+1)){ return mid; } left = mid + 1; } } } }
public class Solution { public int mysqrt (int x) { if(x <= 1) return x; int left = 1; int right = x / 2; while(left <= right){ int mid = left + (right - left) / 2; if(mid == x / mid){ return mid; }else if(mid < (x / mid)){ if((mid + 1) * (mid + 1) > x){ return mid; }else{ left = mid + 1; } }else{ right = mid - 1; } } return -1; } }
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; } }
主要考察小细节:
1、直接相乘会导致溢出
2、什么地方该加一,什么地方不该加一
import java.util.*; public class Solution { /** * * @param x int整型 * @return int整型 */ public int mysqrt (int x) { if (x<=1) return x; int l=0,r=x; while (l<=r) { int mid = (l + r) / 2; if (mid<=x/mid && (mid+1)>x/(mid+1)) return mid; if (mid>x/mid) r=mid-1; else l=mid+1; } return -1; } }
import java.util.*; public class Solution { /** * * @param x int整型 * @return int整型 */ public int mysqrt (int x) { // write code here if(x==0) return 0; int left=1,right=x; // while(left<=right){ while(true){ int mid=left+(right-left)/2; if(mid>x/mid){ right=mid-1; }else{ if((mid+1)>x/(mid+1)) return mid; left=mid+1; } } } }
// 我写的什么玩意 public class Solution { /** * * @param x int整型 * @return int整型 */ public int mysqrt (int x) { // write code here if(x<=1) return x; int mid=x/2; int high=0, low=0; while(mid>0){ // 不能写成mid*mid>x 因为会越界 if(mid>x/mid){ mid=mid/2; }else if(mid < x/mid){ low=mid; high=mid*2; break; }else{ return mid; } } //System.out.println("high:"+high); //System.out.println("low:"+low); for(int i=high; i>=low;i--){ if(i>x/i){ continue; }else{ return i; } } return -1; } }
import java.util.*; public class Solution { /** * * @param x int整型 * @return int整型 */ public int mysqrt (int x) { // write code here long ans = 0L; long L = 1L; long R = x; while (L <= R) { long M = L + ((R - L) >> 1); if (M * M <= x) { ans = M; L = M + 1; } else { R = M - 1; } } return (int) ans; } }
public class Solution { /** * * @param x int整型 * @return int整型 */ public int mysqrt (int x) { int l = 1; int r = x; while (l <= r) { int mid = l + r >>> 1; if (mid > x / mid) { r = mid - 1; } else { l = mid + 1; } } return r; } }
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; } }
public int mysqrt (int x) { boolean flag = false; Long temp = Long.parseLong(Integer.toString(x)); Long dep = 0L; while(!flag){ if(temp*temp <= x && (temp+1)*(temp+1)>x){ flag = true; }else if(temp*temp > x) { dep = temp; temp=temp/2; }else if(temp*temp < x) { temp = (dep + temp) / 2; } } return Integer.parseInt(temp.toString()); }