Skip to content

Commit c147885

Browse files
committed
二刷35
1 parent 5f10b1d commit c147885

File tree

6 files changed

+95
-31
lines changed

6 files changed

+95
-31
lines changed

docs/0000-00-note.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ image::images/quick-sort-01.gif[{image_attr}]
2929
* [ ] 通过 xref:2560-house-robber-iv.adoc[2560. House Robber IV] 发现,竟然还有「最大化最小值」或「最小化最大值」的二分答案问题。
3030
* [ ] KMP 算法
3131
* [ ] 可以使用类库的,却总想自己写代码实现!必然字符串比较!
32+
* [ ] 在 https://leetcode.cn/problems/search-insert-position/solutions/2023391/er-fen-cha-zhao-zong-shi-xie-bu-dui-yi-g-nq23/[35. 搜索插入位置 - 二分查找总是写不对?一个视频讲透!^] 中,作者说二分查找“在找到 target 就立刻返回”,这种写法适用范围很窄,多思考体会一下。
3233

3334
== 感慨
3435

docs/0000-01-modified-binary-search.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ image::images/modified-binary-search.jpeg[{image_attr}]
1515
== 疑问点
1616

1717
. 二分搜索怎样可以确定地取左右端点?
18+
. 在 https://leetcode.cn/problems/search-insert-position/solutions/2023391/er-fen-cha-zhao-zong-shi-xie-bu-dui-yi-g-nq23/[35. 搜索插入位置 - 二分查找总是写不对?一个视频讲透!^] 中,作者说“在找到 target 就立刻返回”,这种写法适用范围很窄,多思考体会一下。
1819

1920
== 经典题目
2021

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,71 @@
11
[#0035-search-insert-position]
2-
= 35. Search Insert Position
2+
= 35. 搜索插入位置
33

4-
{leetcode}/problems/search-insert-position/[LeetCode - Search Insert Position^]
4+
https://leetcode.cn/problems/search-insert-position/[LeetCode - 35. 搜索插入位置^]
55

6-
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
6+
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
77

8-
You may assume no duplicates in the array.
8+
请必须使用时间复杂度为 stem:[O(log n)] 的算法。
99

10-
*Example 1:*
10+
*示例 1:*
1111

12-
[subs="verbatim,quotes,macros"]
13-
----
14-
*Input:* [1,3,5,6], 5
15-
*Output:* 2
16-
----
12+
....
13+
输入: nums = [1,3,5,6], target = 5
14+
输出: 2
15+
....
1716

18-
*Example 2:*
17+
*示例 2:*
1918

20-
[subs="verbatim,quotes,macros"]
21-
----
22-
*Input:* [1,3,5,6], 2
23-
*Output:* 1
24-
----
19+
....
20+
输入: nums = [1,3,5,6], target = 2
21+
输出: 1
22+
....
2523

26-
*Example 3:*
24+
*示例 3:*
2725

28-
[subs="verbatim,quotes,macros"]
29-
----
30-
*Input:* [1,3,5,6], 7
31-
*Output:* 4
32-
----
26+
....
27+
输入: nums = [1,3,5,6], target = 7
28+
输出: 4
29+
....
3330

34-
*Example 4:*
31+
*提示:*
32+
33+
* `1 \<= nums.length \<= 10^4^`
34+
* `-10^4^ \<= nums[i] \<= 10^4^`
35+
* `nums`**无重复元素****升序**排列数组
36+
* `-10^4^ \<= target \<= 10^4^`
3537
36-
[subs="verbatim,quotes,macros"]
37-
----
38-
*Input:* [1,3,5,6], 0
39-
*Output:* 0
40-
----
4138
4239
40+
== 思路分析
41+
42+
image::images/modified-binary-search.jpeg[{image_attr}]
43+
4344
[[src-0035]]
45+
[tabs]
46+
====
47+
一刷::
48+
+
49+
--
4450
[{java_src_attr}]
4551
----
4652
include::{sourcedir}/_0035_SearchInsertPosition.java[tag=answer]
4753
----
54+
--
55+
56+
二刷::
57+
+
58+
--
59+
[{java_src_attr}]
60+
----
61+
include::{sourcedir}/_0035_SearchInsertPosition_2.java[tag=answer]
62+
----
63+
--
64+
====
65+
66+
67+
== 参考资料
4868

69+
. https://leetcode.cn/problems/search-insert-position/solutions/2023391/er-fen-cha-zhao-zong-shi-xie-bu-dui-yi-g-nq23/[35. 搜索插入位置 - 二分查找总是写不对?一个视频讲透!^]
70+
. https://leetcode.cn/problems/search-insert-position/solutions/10969/te-bie-hao-yong-de-er-fen-cha-fa-fa-mo-ban-python/[35. 搜索插入位置 - 尽可能讲到大家对于二分查找可能遇到的问题,结尾有题单(Java)^]
71+
. https://leetcode.cn/problems/search-insert-position/solutions/536357/yi-wen-dai-ni-gao-ding-er-fen-cha-zhao-j-69ao/[35. 搜索插入位置 - 一文带你搞定二分查找及其多个变种!^]

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,6 +1999,11 @@ endif::[]
19991999
|{doc_base_url}/0039-combination-sum.adoc[题解]
20002000
|✅ 回溯。使用回溯解法,要注意剪枝技巧。这道题还是一个“完全背包”问题,也可以尝试一下完全背包的解法。
20012001

2002+
|{counter:codes2503}
2003+
|{leetcode_base_url}/search-insert-position/[35. 搜索插入位置^]
2004+
|{doc_base_url}/0035-search-insert-position.adoc[题解]
2005+
|✅ 二分查找。主要学习二分查找的各种变种解法。解答中提到“在找到 target 就立刻返回”,这种写法适用范围很窄,多思考体会一下。
2006+
20022007
|===
20032008

20042009
截止目前,本轮练习一共完成 {codes2503} 道题。

src/main/java/com/diguage/algo/leetcode/_0035_SearchInsertPosition.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@
4242
* @since 2018-09-16 21:32
4343
*/
4444
public class _0035_SearchInsertPosition {
45-
// tag::answer[]
45+
// tag::answer[]
46+
/**
47+
* @author D瓜哥 · https://www.diguage.com
48+
* @since 2018-09-16 21:32
49+
*/
4650
public static int searchInsert(int[] nums, int target) {
4751
if (null == nums || nums.length == 0) {
4852
return 0;
@@ -77,8 +81,7 @@ public static int searchInsert(int[] nums, int target) {
7781

7882
return result;
7983
}
80-
81-
// end::answer[]
84+
// end::answer[]
8285

8386

8487
public static void main(String[] args) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0035_SearchInsertPosition_2 {
4+
// tag::answer[]
5+
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-11-26 21:03:14
9+
*/
10+
public int searchInsert(int[] nums, int target) {
11+
int left = 0, right = nums.length - 1;
12+
while (left <= right) {
13+
// 循环不变量:
14+
// nums[left-1] < target
15+
// nums[right+1] >= target
16+
int mid = left + (right - left) / 2;
17+
int num = nums[mid];
18+
if (num < target) {
19+
left = mid + 1;
20+
} else {
21+
right = mid - 1;
22+
}
23+
}
24+
return right + 1; // 这里使用 left 也可以
25+
}
26+
// end::answer[]
27+
static void main() {
28+
new _0035_SearchInsertPosition_2()
29+
.searchInsert(new int[]{1, 3, 5, 6}, 2);
30+
}
31+
}

0 commit comments

Comments
 (0)