liuchuo / LeetCode

🍡 LeetCode Online Judge刷题题解(Java/C++/Python/Ruby/Swift)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Leetcode69 sqrt(x)有个小问题~

Chuncheng666 opened this issue · comments

首先我用的是Visual Studio 2015,之前觉得这题很水,直接写了一个for(int i=0;i<=x;i++)的循环,然后超时,来看了柳神的代码,发现可以使用二分法,我也根据你的long来的,但是发现还是卡住不出结果,所以我就单步调试了一下,发现mid和left都变成负的很大的数字了,看着像是溢出,这时候我把mid改成long long类型,就迅速的返回了结果~ 还有一个地方在最外面定义了long mid =0,循环里面又定义long mid啦~~

sizeof(long int)=sizeof(int)=sizeof(long)