思路:
二分法
总结:
ull != int * int
代码:
#include <iostream> using namespace std; class Solution { public: int sqrt(int x) { unsigned long long l = 0, r = x, mid; unsigned long long multi; while(l < r ) { mid = (l+r) >> 1; multi = mid * mid; if(multi == x) { return mid; }else if(multi > x) { r = mid -1; }else { l = mid + 1; } } multi = l * l; if(multi > x) return l-1; else return l; } }; int main() { Solution solution; cout << solution.sqrt(2147395599) << endl; return 0; }