Skip to content

Commit c06ff28

Browse files
committed
✨feat: Add 278
1 parent 574d77c commit c06ff28

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

Index/二分.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
| [153. 寻找旋转排序数组中的最小值](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/solution/gong-shui-san-xie-yan-ge-olognyi-qi-kan-6d969/) | 中等 | 🤩🤩🤩 |
1111
| [154. 寻找旋转排序数组中的最小值 II](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/solution/gong-shui-san-xie-xiang-jie-wei-he-yuan-7xbty/) | 困难 | 🤩🤩🤩 |
1212
| [220. 存在重复元素 III](https://leetcode-cn.com/problems/contains-duplicate-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/contains-duplicate-iii/solution/gong-shui-san-xie-yi-ti-shuang-jie-hua-d-dlnv/) | 中等 | 🤩🤩🤩 |
13+
| [278. 第一个错误的版本](https://leetcode-cn.com/problems/first-bad-version/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/first-bad-version/solution/gong-shui-san-xie-shi-yong-jiao-hu-han-s-8hpv/) | 简单 | 🤩🤩🤩🤩 |
1314
| [354. 俄罗斯套娃信封问题](https://leetcode-cn.com/problems/russian-doll-envelopes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/russian-doll-envelopes/solution/zui-chang-shang-sheng-zi-xu-lie-bian-xin-6s8d/) | 困难 | 🤩🤩🤩 |
1415
| [363. 矩形区域不超过 K 的最大数值和](https://leetcode-cn.com/problems/max-sum-of-rectangle-no-larger-than-k/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/max-sum-of-rectangle-no-larger-than-k/solution/gong-shui-san-xie-you-hua-mei-ju-de-ji-b-dh8s/) | 困难 | 🤩🤩🤩 |
1516
| [778. 水位上升的泳池中游泳](https://leetcode-cn.com/problems/swim-in-rising-water/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/swim-in-rising-water/solution/gong-shui-san-xie-yi-ti-shuang-jie-krusk-7c6o/) | 困难 | 🤩🤩🤩 |
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[278. 第一个错误的版本](https://leetcode-cn.com/problems/first-bad-version/solution/gong-shui-san-xie-shi-yong-jiao-hu-han-s-8hpv/)** ,难度为 **简单**
4+
5+
Tag : 「二分」
6+
7+
8+
9+
你是产品经理,目前正在带领一个团队开发新的产品。不幸的是,你的产品的最新版本没有通过质量检测。由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的。
10+
11+
假设你有 `n` 个版本 `[1, 2, ..., n]`,你想找出导致之后所有版本出错的第一个错误的版本。
12+
13+
你可以通过调用 `bool isBadVersion(version)` 接口来判断版本号 `version` 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 `API` 的次数。
14+
15+
示例:
16+
```
17+
给定 n = 5,并且 version = 4 是第一个错误的版本。
18+
19+
调用 isBadVersion(3) -> false
20+
调用 isBadVersion(5) -> true
21+
调用 isBadVersion(4) -> true
22+
23+
所以,4 是第一个错误的版本。 
24+
```
25+
26+
---
27+
28+
### 二分
29+
30+
一道交互题,根据题意可知,是尽可能少调用 `isBadVersion` 方法来找到分割点。
31+
32+
考虑存在「**没有错误版本**」和「**全是错误版本**」的情况,但如果往头部插入一个正确版本,往尾部插入一个错误版本作为哨兵,仍然具有「**二段性**」。
33+
34+
实际上,只需要进行这样的思考即可,不需要真正插入这样的哨兵,把这个哨兵逻辑放到最后返回的时候判断一下即可。
35+
36+
那么只需要将 `isBadVersion` 当做 `check` 函数进行二分即可。
37+
38+
**二分通常有以下两种写法,分别代表「找到最靠近中心的 `True`」 和「找到最靠近中心的 `False`」。**
39+
40+
另外根据数据范围需要注意计算 `mid` 时的爆 `int` 问题,可以通过使用类似 `l + (r - l) / 2` 的做法解决,也可以通过一个临时 `long` 来解决。
41+
42+
代码:
43+
```Java []
44+
public class Solution extends VersionControl {
45+
public int firstBadVersion(int n) {
46+
int l = 1, r = n;
47+
while (l < r) {
48+
long tmp = (long)l + r >> 1;
49+
int mid = (int)tmp;
50+
if (isBadVersion(mid)) {
51+
r = mid;
52+
} else {
53+
l = mid + 1;
54+
}
55+
}
56+
return r;
57+
}
58+
}
59+
```
60+
```Java []
61+
public class Solution extends VersionControl {
62+
public int firstBadVersion(int n) {
63+
int l = 1, r = n;
64+
while (l < r) {
65+
long tmp = (long)l + r + 1 >> 1;
66+
int mid = (int)tmp;
67+
if (!isBadVersion(mid)) {
68+
l = mid;
69+
} else {
70+
r = mid - 1;
71+
}
72+
}
73+
return !isBadVersion(r) ? r + 1 : r;
74+
}
75+
}
76+
```
77+
* 时间复杂度:$O(\log{n})$
78+
* 空间复杂度:$O(1)$
79+
80+
---
81+
82+
### 其他「二分」相关题解
83+
84+
* 二分模板
85+
[29. 两数相除](https://leetcode-cn.com/problems/divide-two-integers/) : [二分 + 倍增乘法解法(含模板)](https://leetcode-cn.com/problems/divide-two-integers/solution/shua-chuan-lc-er-fen-bei-zeng-cheng-fa-j-m73b/)
86+
87+
* 二分本质 & 恢复二段性处理
88+
89+
[33. 搜索旋转排序数组(找目标值)](https://leetcode-cn.com/problems/search-in-rotated-sorted-array/) : [严格 O(logN),一起看清二分的本质](https://leetcode-cn.com/problems/search-in-rotated-sorted-array/solution/shua-chuan-lc-yan-ge-ologn100yi-qi-kan-q-xifo/)
90+
91+
[81. 搜索旋转排序数组 II(找目标值)](https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/) : [详解为何元素相同会导致 O(n),一起看清二分的本质](https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/solution/gong-shui-san-xie-xiang-jie-wei-he-yuan-xtam4/)
92+
93+
[153. 寻找旋转排序数组中的最小值(找最小值)](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/) : [严格 O(logN),一起看清二分的本质](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/solution/gong-shui-san-xie-yan-ge-olognyi-qi-kan-6d969/)
94+
95+
[154. 寻找旋转排序数组中的最小值 II(找最小值)](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/) : [详解为何元素相同会导致 O(n),一起看清二分的本质](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/solution/gong-shui-san-xie-xiang-jie-wei-he-yuan-7xbty/)
96+
97+
* 二分 check 函数如何确定
98+
[34. 在排序数组中查找元素的第一个和最后一个位置](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/) : [考察对「二分」的理解,以及 check 函数的「大于 小于」怎么写](https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/solution/gong-shui-san-xie-kao-cha-dui-er-fen-de-86bk0/)
99+
100+
---
101+
102+
### 最后
103+
104+
这是我们「刷穿 LeetCode」系列文章的第 `No.278` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先将所有不带锁的题目刷完。
105+
106+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
107+
108+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
109+
110+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
111+

0 commit comments

Comments
 (0)