标签:root class mys compute log style turn return 时间复杂度
Implement int sqrt(int x)
.
Compute and return the square root of x.
像这种问题,不要求时间复杂度就能过OJ就有鬼了。。
二分搜索法
public int mySqrt(int x) { long left = 0,right = x; long t = x; while(left<=right) { long mid = left+(right-left)/2; if(mid*mid<=t&&(mid+1)*(mid+1)>t) return (int)mid; else if(mid*mid<x) left = mid + 1; else if(mid*mid>x) right = mid; } return 0; }
标签:root class mys compute log style turn return 时间复杂度
原文地址:http://www.cnblogs.com/swuwyb/p/7749760.html