GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — [LeetCode] 278. First Bad Version · Issue #278 · grandyang/leetcode Open Description
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 You are given an API
Example 1: Example 2:
Constraints:
Credits:
话说这个叫李建超(音译)的哥们太赞了,自从 LeetCode 开始收费后,大多数的免费题都是这哥们出的,还写了所有的 test cases,32 个赞。这道题说是有一系列版本,其中有一个版本是坏的,而且后面跟着的全是坏的,给了一个 API 函数可以用来判定当前版本是否是坏的,让我们尽可能少的调用这个 API,找出第一个坏版本。那么这种搜索题最先开始考虑用二分查找法把,效率高嘛。由于这题很有规律,好版本和坏版本一定有个边界,那么用二分法来找这个边界,对 mid 值调用API函数,如果是坏版本,说明边界在左边,则把 mid 赋值给 right,如果是好版本,则说明边界在右边,则把 mid+1 赋给 left,最后返回 left 即可。需要注意的是,OJ 里有个坑,那就是如果 left 和 right 都特别大的话,那么 left+right 可能会溢出,这里处理方法就是变成 left + (right - left) / 2,很好的避免的溢出问题,参见代码如下:
如果习惯了二分搜索法从0开始找,可以用下面的方法:
Github 同步地址:
参考资料: https://leetcode.com/problems/first-bad-version/
LeetCode All in One 题目讲解汇总(持续更新中...) 喜欢请点赞,疼爱请打赏❤️~.~ 微信打赏 Venmo 打赏 ActivityMetadataMetadataAssigneesLabelsNo labels TypeProjectsMilestoneRelationshipsDevelopmentIssue actions |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |