C# solution using binary search


  • 0
    K
    public class Solution : VersionControl {
        public int FirstBadVersion(int n) {
            
            decimal first = 1;
            decimal last = n;
            int mid = 1;
            int lastbad = 1;
            
            while (first <= last)
            {
                mid = Convert.ToInt32(Math.Round((first+last)/2));
    
                if (IsBadVersion(mid))
                {
                    last = mid-1;
                    lastbad = mid;
                }
                else
                {
                    first = mid+1;
                }
            }
            return lastbad;
        }
    }
    

Log in to reply
 

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