solution.cpp 414 字节
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
	int mySqrt(int x)
	{
		if (x == 0)
		{
			return 0;
		}
		unsigned int lo = 1, hi = x;
		unsigned int mid = (lo + hi) / 2;
		for (;;)
		{
			if (mid > x / mid)
			{
				hi = mid;
			}
			else
			{
				if (mid + 1 > x / (mid + 1))
				{
					break;
				}
				else
				{
					lo = mid;
				}
			}
			mid = (lo + hi) / 2;
		}
		return mid;
	}
};