Share code that beats 97.3%


  • 0
    B
    public class Solution : VersionControl {
        public int FirstBadVersion(int n) {
             var begin = 1;
                var end = n;
                if (IsBadVersion(1)) return 1;
                while (true)
                {
                    var current = begin + (end - begin)/2;
                    var post = current + 1;
                    if (current == n) return n;
                    var result = IsBadVersion(current);
                    var postResult = IsBadVersion(post);
                    if (!result && postResult) return post;
                    if (result)
                    {
                        end = current;
                    }
                    if (!postResult)
                    {
                        begin = post;
                    }
                }
        }
    }
    
    

Log in to reply
 

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