# Fastest C++Solution considering all situations

• First,This question has some problems itself:the function of guess() returns 0,-1,1,which is refered to equal,higher and lower,but not suited to the discribed ones.
Moreover,some solutions haven't considered the periphery.I got the complete one below.

``````int guess(int num);

class Solution {
public:
int guessNumber(int n) {
if(n<1)
return 0;
if(n==1)
return 1;
long left=1,right=n,flag,mid;
int guess(int num);

while(left<right)
{
mid=(right+left)/2;
flag=guess(mid);
if(flag==0)
return mid;
if(flag==-1)
{
right=mid;
if(left+1==right)
return mid=left;
}
if(flag==1)
{
left=mid;
if(left+1==right)
return mid=right;
}
}
return mid;
}
};
``````

• there are might be a mistake in your answer. considering "mid = (right + left) / 2" this might cause overflow

• @forest_open It won't overflow thanks to the type of variable "mid" is long,it can bear the add of two int type.

• This post is deleted!

• @forest_open It won't overflow thanks to the type of variable "mid" is long,it can bear the type of integer n .

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.