File tree Expand file tree Collapse file tree 4 files changed +47
-0
lines changed
src/main/java/com/diguage/algo/leetcode Expand file tree Collapse file tree 4 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -35,11 +35,29 @@ Given a *sorted* (in ascending order) integer array `nums` of `n` elements and a
35
35
36
36
image::images/0704-01.gif[{image_attr}]
37
37
38
+ image::images/0704-02.gif[{image_attr}]
39
+
38
40
[[src-0704]]
41
+ [tabs]
42
+ ====
43
+ 一刷::
44
+ +
45
+ --
39
46
[{java_src_attr}]
40
47
----
41
48
include::{sourcedir}/_0704_BinarySearch.java[tag=answer]
42
49
----
50
+ --
51
+
52
+ 二刷::
53
+ +
54
+ --
55
+ [{java_src_attr}]
56
+ ----
57
+ include::{sourcedir}/_0704_BinarySearch_2.java[tag=answer]
58
+ ----
59
+ --
60
+ ====
43
61
44
62
== 参考资料
45
63
Original file line number Diff line number Diff line change 560
560
|{doc_base_url} /0054-spiral-matrix.adoc[题解]
561
561
|从回溯得到启发,使用递归来完成层次的遍历。
562
562
563
+ |{counter:codes}
564
+ |{leetcode_base_url} /binary-search/[704. Binary Search^]
565
+ |{doc_base_url} /0704-binary-search.adoc[题解]
566
+ |二分查找
567
+
563
568
|===
564
569
565
570
截止目前,本轮练习一共完成 {codes} 道题。
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments