278. First Bad Version Easy
1/**
2 * [278] First Bad Version
3 *
4 * 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.
5 * 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.
6 * You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
7 *
8 * Example 1:
9 *
10 * Input: n = 5, bad = 4
11 * Output: 4
12 * Explanation:
13 * call isBadVersion(3) -> false
14 * call isBadVersion(5) -> true
15 * call isBadVersion(4) -> true
16 * Then 4 is the first bad version.
17 *
18 * Example 2:
19 *
20 * Input: n = 1, bad = 1
21 * Output: 1
22 *
23 *
24 * Constraints:
25 *
26 * 1 <= bad <= n <= 2^31 - 1
27 *
28 */
29pub struct Solution {}
30
31// problem: https://leetcode.com/problems/first-bad-version/
32// discuss: https://leetcode.com/problems/first-bad-version/discuss/?currentPage=1&orderBy=most_votes&query=
33
34// submission codes start here
35
36// The API isBadVersion is defined for you.
37// isBadVersion(version:i32)-> bool;
38// to call it use self.isBadVersion(version)
39
40impl Solution {
41 pub fn first_bad_version(&self, n: i32) -> i32 {
42
43 }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 #[test]
53 fn test_278() {
54 }
55}
56
Back
© 2025 bowen.ge All Rights Reserved.