File tree Expand file tree Collapse file tree
src/main/java/com/diguage/algo/leetcode Expand file tree Collapse file tree 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
3535
3636image::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----
4148include::{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
Original file line number Diff line number Diff line change 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} 道题。
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