Need help on my java code


  • 0
    A

    I have two solutions, one is using "mid = start + (end - start) / 2;", passed; another is using "mid = (start + end) / 2;" timed out.

    I really don't have clue what's going on.

    Any suggestion is greatly helpful

    public int firstBadVersion(int n) {

        int start = 1;
        int end = n;
        while(start < end){
            int mid = start + (end - start) / 2; //(start + end)/2;
            if(isBadVersion(mid)){
                end = mid;
            }else{
                start = mid + 1;
            }
        }
        
        return start;
    }

  • 2
    Z

    start+end might overflow, while start-end could be a small number.


Log in to reply
 

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