0ms C++ Solution


  • 0
    Y
    // Forward declaration of isBadVersion API.
    bool isBadVersion(int version);
    
    class Solution {
    public:
        int firstBadVersion(int n) {
        	int start = 0, end = n;
        	n = start / 2 + end / 2;
        	while (start != end) {
        		if (isBadVersion(n))
        			end = n;
        		else
        			start = n + 1;
        		n = start / 2 + end / 2;
        	}
        	return start;
        }
    };

  • 0
    L

    Can someone explain why changing n = start / 2 + end / 2 to n = (start + end)/2 exceed the runtime limit?


Log in to reply
 

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