[LeetCode] First Bad Version

简介: Just use binary search to find the first bad version. The code is as follows. 1 // Forward declaration of isBadVersion API.

Just use binary search to find the first bad version. The code is as follows.

 1 // Forward declaration of isBadVersion API.
 2 bool isBadVersion(int version);
 3 
 4 class Solution {
 5 public:
 6     int firstBadVersion(int n) {
 7         int l = 1, r = n;
 8         while (l < r) {
 9             int m = l + (r - l) / 2;
10             if (isBadVersion(m)) r = m;
11             else l = m + 1;
12         }
13         return l;
14     }
15 };

 

目录
相关文章
|
索引
LeetCode 387. First Unique Character in a String
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
104 0
LeetCode 387. First Unique Character in a String
|
测试技术 API
LeetCode 278. First Bad Version
假设你有 n 个版本 [1, 2, ..., n],你想找出导致之后所有版本出错的第一个错误的版本。 你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。
57 0
LeetCode 278. First Bad Version
|
测试技术 API
LeetCode 278. 第一个错误的版本 First Bad Version
LeetCode 278. 第一个错误的版本 First Bad Version
|
数据库
LeetCode(数据库)- First and Last Call On the Same Day
LeetCode(数据库)- First and Last Call On the Same Day
113 0
LeetCode之First Unique Character in a String
LeetCode之First Unique Character in a String
116 0
|
Java 索引 Python
LeetCode 387: 字符串中的第一个唯一字符 First Unique Character in a String
题目: 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。 Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. 注意事项:您可以假定该字符串只包含小写字母。
772 0