Skip to content

Commit e9671ba

Browse files
committed
二刷704
1 parent 36b78da commit e9671ba

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

docs/0704-binary-search.adoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,29 @@ Given a *sorted* (in ascending order) integer array `nums` of `n` elements and a
3535

3636
image::images/0704-01.gif[{image_attr}]
3737

38+
image::images/0704-02.gif[{image_attr}]
39+
3840
[[src-0704]]
41+
[tabs]
42+
====
43+
一刷::
44+
+
45+
--
3946
[{java_src_attr}]
4047
----
4148
include::{sourcedir}/_0704_BinarySearch.java[tag=answer]
4249
----
50+
--
51+
52+
二刷::
53+
+
54+
--
55+
[{java_src_attr}]
56+
----
57+
include::{sourcedir}/_0704_BinarySearch_2.java[tag=answer]
58+
----
59+
--
60+
====
4361

4462
== 参考资料
4563

docs/images/0704-02.gif

6.52 MB
Loading

logbook/202406.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,11 @@
560560
|{doc_base_url}/0054-spiral-matrix.adoc[题解]
561561
|从回溯得到启发,使用递归来完成层次的遍历。
562562

563+
|{counter:codes}
564+
|{leetcode_base_url}/binary-search/[704. Binary Search^]
565+
|{doc_base_url}/0704-binary-search.adoc[题解]
566+
|二分查找
567+
563568
|===
564569

565570
截止目前,本轮练习一共完成 {codes} 道题。
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0704_BinarySearch_2 {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2024-09-14 19:52:26
8+
*/
9+
public int search(int[] nums, int target) {
10+
int left = 0, right = nums.length - 1;
11+
while (left <= right) {
12+
int mid = left + (right - left) / 2;
13+
if (nums[mid] == target) {
14+
return mid;
15+
} else if (nums[mid] < target) {
16+
left = mid + 1;
17+
} else {
18+
right = mid - 1;
19+
}
20+
}
21+
return -1;
22+
}
23+
// end::answer[]
24+
}

0 commit comments

Comments
 (0)