完成数组二分查找,以及范围查找
package com.deng.b;
class Solution {
public static int mySqrt(int x) {
int left= 0;
int right =x;
while(left <=right ){
int mid = (right - left)/2 +left;
long val = mid * mid;
if(val == x){
return mid;
}else if (val > x){
right = mid -1;
}else if(val < x){
left = mid +1;
}
}
if(left * left == x){
return left;
}
return left-1;
}
public static void main(String[] args) {
System.out.println(mySqrt(Integer.MAX_VALUE));
}
}