Click here to see the full article post
because "Unless you are using a language that does not overflow such as Python, left + rightleft+right could overflow."
Same question here, why mid = (left+ right)/2 doesn't work?, @mscho147 could you please explain why left+right could overflow?
If you want to do (left + right)/2, you have to cast in order to make the overflow work correct. In that case you would have to do:
mid = (int)(((long)left + right)/2);
Clearly this is more verbose and error prone than simply doing left + (right-left)/2;
@webmihir long L,R; there only one cast when calling isbadversion, and second when returning.
Can someone please explain to me why sometimes we use the terminating condition
while(start<end) while some other times we use
while(start<=end) . I am not able to grok this thing and it causes too much confusion and frustration.An explanation to this would be very highly appreciated. Thank you!
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.