Skip to content

Commit 630d653

Browse files
committed
二刷34
1 parent a83677a commit 630d653

5 files changed

+138
-13
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[#0000-01-modified-binary-search]
2-
= Modified Binary Search 改造过的二分搜索
2+
= Modified Binary Search 改进的二分搜索
33

44
当你需要解决的问题的输入是排好序的数组,链表,或是排好序的矩阵,要求咱们寻找某些特定元素。这个时候的不二选择就是二分搜索。这种模式是一种超级牛的用二分来解决问题的方式。
55

docs/0034-find-first-and-last-position-of-element-in-sorted-array.adoc

+42-11
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,70 @@
11
[#0034-find-first-and-last-position-of-element-in-sorted-array]
2-
= 34. Find First and Last Position of Element in Sorted Array
2+
= 34. 在排序数组中查找元素的第一个和最后一个位置
33

4-
{leetcode}/problems/find-first-and-last-position-of-element-in-sorted-array/[LeetCode - Find First and Last Position of Element in Sorted Array^]
4+
https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/[LeetCode - 34. 在排序数组中查找元素的第一个和最后一个位置 ^]
55

6-
Given an array of integers `nums` sorted in ascending order, find the starting and ending position of a given `target` value.
6+
给你一个按照非递减顺序排列的整数数组 `nums`,和一个目标值 `target`。请你找出给定目标值在数组中的开始位置和结束位置。
77

8-
Your algorithm's runtime complexity must be in the order of _O_(log _n_).
8+
如果数组中不存在目标值 `target`,返回 `[-1, -1]`
99

10-
If the target is not found in the array, return `[-1, -1]`.
10+
你必须设计并实现时间复杂度为 stem:[log_2n] 的算法解决此问题。
1111

12-
*Example 1:*
12+
*示例 1:*
1313

1414
[subs="verbatim,quotes,macros"]
1515
----
16-
*Input:* nums = [`5,7,7,8,8,10]`, target = 8
17-
*Output:* [3,4]
16+
输入:nums = [5,7,7,8,8,10], target = 8
17+
输出:[3,4]
1818
----
1919

20-
*Example 2:*
20+
*示例 2:*
2121

2222
[subs="verbatim,quotes,macros"]
2323
----
24-
*Input:* nums = [`5,7,7,8,8,10]`, target = 6
25-
*Output:* [-1,-1]
24+
输入:nums = [5,7,7,8,8,10], target = 6
25+
输出:[-1,-1]
2626
----
2727

28+
*示例 3:*
29+
30+
[subs="verbatim,quotes,macros"]
31+
----
32+
输入:nums = [], target = 0
33+
输出:[-1,-1]
34+
----
35+
36+
*提示:*
37+
38+
* `+0 <= nums.length <= 10+`^`+5+`^
39+
* `-10`^`9`^`+<= nums[i] <= 10+`^`9`^
40+
* `nums` 是一个非递减数组
41+
* `-10`^`9`^`+<= target <= 10+`^`9`^
42+
2843
== 思路分析
2944

3045
可以直接使用二分查找,搜索两次。
3146

3247
[[src-0034]]
48+
[tabs]
49+
====
50+
一刷::
51+
+
52+
--
3353
[{java_src_attr}]
3454
----
3555
include::{sourcedir}/_0034_FindFirstAndLastPositionOfElementInSortedArray.java[tag=answer]
3656
----
57+
--
58+
59+
二刷::
60+
+
61+
--
62+
[{java_src_attr}]
63+
----
64+
include::{sourcedir}/_0034_FindFirstAndLastPositionOfElementInSortedArray_2.java[tag=answer]
65+
----
66+
--
67+
====
3768

3869
== 参考资料
3970

logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ endif::[]
1515
|{doc_base_url}/0206-reverse-linked-list.adoc[题解]
1616
|⭕️ 递归解法非常妙!传一个参数,`next``next` 比较麻烦,传两个参数比较简单。
1717

18+
|{counter:codes2503}
19+
|{leetcode_base_url}/find-first-and-last-position-of-element-in-sorted-array/[34. Find First and Last Position of Element in Sorted Array^]
20+
|{doc_base_url}/0034-find-first-and-last-position-of-element-in-sorted-array.adoc[题解]
21+
|✅ 思考清楚确定边界时,中间指针的移动方向即可迎刃而解。
22+
1823

1924
|===
2025

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
*/
3232
public class _0034_FindFirstAndLastPositionOfElementInSortedArray {
3333
// tag::answer[]
34-
public static int[] searchRange(int[] nums, int target) {
34+
/**
35+
* @author D瓜哥 · https://www.diguage.com
36+
* @since 2018-09-16 20:50 初次完成,2024-07-01 17:24:23 优化
37+
*/
38+
public int[] searchRange(int[] nums, int target) {
3539
if (nums == null || nums.length == 0) {
3640
return new int[]{-1, -1};
3741
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.diguage.algo.leetcode;
2+
3+
4+
public class _0034_FindFirstAndLastPositionOfElementInSortedArray_2 {
5+
// tag::answer[]
6+
/**
7+
* @author D瓜哥 · https://www.diguage.com
8+
* @since 2025-03-04 21:02:59
9+
*/
10+
public int[] searchRange(int[] nums, int target) {
11+
int left = binarySearch(nums, target, true);
12+
if (left == -1) {
13+
return new int[]{-1, -1};
14+
}
15+
int right = binarySearch(nums, target, false);
16+
return new int[]{left, right};
17+
}
18+
19+
private int binarySearch(int[] nums, int target, boolean isLeft) {
20+
int left = 0;
21+
int right = nums.length - 1;
22+
// 使用 result 变量,省去很多繁琐的判断
23+
int result = -1;
24+
while (left <= right) {
25+
int mid = left + (right - left) / 2;
26+
if (nums[mid] < target) {
27+
left = mid + 1;
28+
} else if (target < nums[mid]) {
29+
right = mid - 1;
30+
} else {
31+
if (isLeft) {
32+
// 注意:找左边界,要收缩右指针
33+
right = mid - 1;
34+
} else {
35+
// 注意:找右边界,要搜索左指针
36+
left = mid + 1;
37+
}
38+
result = mid;
39+
}
40+
}
41+
return result;
42+
}
43+
44+
// 下面是原始代码,上面是优化后的代码
45+
private int binarySearchLeft(int[] nums, int target) {
46+
int left = 0;
47+
int right = nums.length - 1;
48+
// 使用 result 变量,省去很多繁琐的判断
49+
int result = -1;
50+
while (left <= right) {
51+
int mid = left + (right - left) / 2;
52+
if (nums[mid] < target) {
53+
left = mid + 1;
54+
} else if (target < nums[mid]) {
55+
right = mid - 1;
56+
} else {
57+
// 注意:找左边界,要收缩右指针
58+
right = mid - 1;
59+
result = mid;
60+
}
61+
}
62+
return result;
63+
}
64+
65+
private int binarySearchRight(int[] nums, int target) {
66+
int left = 0;
67+
int right = nums.length - 1;
68+
// 使用 result 变量,省去很多繁琐的判断
69+
int result = -1;
70+
while (left <= right) {
71+
int mid = left + (right - left) / 2;
72+
if (nums[mid] < target) {
73+
left = mid + 1;
74+
} else if (target < nums[mid]) {
75+
right = mid - 1;
76+
} else {
77+
// 注意:找右边界,要搜索左指针
78+
left = mid + 1;
79+
result = mid;
80+
}
81+
}
82+
return result;
83+
}
84+
// end::answer[]
85+
}

0 commit comments

Comments
 (0)