74. First Bad Version(二分法)

本文介绍了一个经典的二分查找算法的应用实例:通过调用接口确定软件仓库中首次出现故障的版本。利用二分查找方法,可以在最少的接口调用次数内找到故障版本。
Description

The code base version is an integer start from 1 to n. One day, someone committed a bad version in the code case, so it caused this version and the following versions are all failed in the unit tests. Find the first bad version.

You can call isBadVersion to help you determine which version is the first bad one. The details interface can be found in the code's annotation part.

 Notice

Please read the annotation in code area to get the correct way to call isBadVersion in different language. For example, Java is SVNRepo.isBadVersion(v)

Example

Given n = 5:

isBadVersion(3) -> false
isBadVersion(5) -> true
isBadVersion(4) -> true

Here we are 100% sure that the 4th version is the first bad version.

Challenge 

You should call isBadVersion as few as possible.

Solution

这道题是经典二分法例题的变形,只不过将查找元素变为了调用接口。

Java

/**
 * public class SVNRepo {
 *     public static boolean isBadVersion(int k);
 * }
 * you can use SVNRepo.isBadVersion(k) to judge whether 
 * the kth code version is bad or not.
*/

public class Solution {
    /*
     * @param n: An integer
     * @return: An integer which is the first bad version.
     */
    public int findFirstBadVersion(int n) {
        // write your code here
        int start = 1, end = n;
        while (start + 1 < end) {
            int mid = start + (end - start) / 2;
            if (SVNRepo.isBadVersion(mid)) {
                end = mid;
            } else {
                start = mid;
            }
        }
        if (SVNRepo.isBadVersion(start)) {
            return start;
        }
        //若不是start位置则一定在end位置,由题意知不存在返回-1的情况
        return end;
    }
}

二分法是一种用于数值计算的迭代算法,用于在一个区间内寻找函数的根。其基本思想是将区间一分为二,然后根据函数在区间两端点的取值情况,确定根位于哪一半区间内,然后继续在该子区间内进行二分,直到满足所需的精度要求。 以下是使用Python实现二分法求解函数 $f(x)=x^3 + x^2 - 1$ 在区间 $[0,1]$ 上的根,精度 $E = 0.0005$ 的代码: ```python def f(x): return x**3 + x**2 - 1 # 二分法函数 def bisection_method(a, b, E): # 检查区间两端点的函数值是否异号 if f(a) * f(b) >= 0: raise ValueError("函数在区间两端点的函数值必须异号。") while (b - a) / 2 > E: # 计算区间的中点 c = (a + b) / 2 # 检查中点是否为根 if f(c) == 0: return c # 根据函数值的符号更新区间 elif f(c) * f(a) < 0: b = c else: a = c # 返回最终的近似根 return (a + b) / 2 # 定义区间和精度 a = 0 b = 1 E = 0.0005 # 调用二分法函数求解根 root = bisection_method(a, b, E) print(f"函数 f(x)=x^3 + x^2 - 1 在区间 [0,1] 上的根,精度 E = 0.0005 的近似值为: {root}") ``` ### 代码解释: 1. **定义函数 $f(x)$**:定义了要求根的函数 $f(x)=x^3 + x^2 - 1$。 2. **二分法函数 `bisection_method`**:该函数接受区间的左右端点 `a` 和 `b`,以及精度 `E` 作为参数。在函数内部,首先检查区间两端点的函数值是否异号,如果不是则抛出异常。然后在循环中,不断将区间一分为二,根据中点的函数值更新区间,直到区间的长度小于等于 $2E$。最后返回区间的中点作为近似根。 3. **调用二分法函数**:定义区间 $[0,1]$ 和精度 $E = 0.0005$,并调用 `bisection_method` 函数求解根。最后打印出近似根的值。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值