题目
代码(首刷看解析 2024年9月8日)
注意相乘越界问题
class Solution {
public:int mySqrt(int x) {if (x == 0) return 0;if (x == 1) return 1;int left = 1;int right = x / 2;while (left < right) {int mid = left + (right - left) / 2;if (mid <= x / mid && (mid + 1) > x / (mid + 1)) {return mid;} else if (mid > x / mid) {right = mid - 1;} else if (mid < x / mid) {left = mid + 1;}}return left;}
};