letcode278[easy]--First Bad Version

来源:互联网 发布:rsyslog linux 编辑:程序博客网 时间:2024/05/15 13:26

难度:easy

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

思路:产品有n个版本,从某位数开始版本出错,其后全部为错,给出一个bool isBadVersion(version)函数用来判断版本正确和错误,找到出错的第一版本号。

     此题用两分法无疑,尝试写了一个总是TLE,于是借鉴别人的。左右两个指针不断缩小第一个函数所在的范围直至所找的元素符合以下特征:或者它就是第一个版本号,或者它是错误版本号且它前一个版本号是正确的版本号。





原创粉丝点击