File tree Expand file tree Collapse file tree 3 files changed +48
-0
lines changed
src/main/java/com/diguage/algo/leetcode Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -68,6 +68,15 @@ include::{sourcedir}/_0033_SearchInRotatedSortedArray.java[tag=answer]
6868include::{sourcedir}/_0033_SearchInRotatedSortedArray_2.java[tag=answer]
6969----
7070--
71+
72+ 三刷::
73+ +
74+ --
75+ [{java_src_attr}]
76+ ----
77+ include::{sourcedir}/_0033_SearchInRotatedSortedArray_3.java[tag=answer]
78+ ----
79+ --
7180====
7281
7382== 参考资料
Original file line number Diff line number Diff line change @@ -160,6 +160,11 @@ endif::[]
160160|{doc_base_url} /0005-longest-palindromic-substring.adoc[题解]
161161|✅ 有更高效的马拉车算法,抽空可以尝试一下。
162162
163+ |{counter:codes2503}
164+ |{leetcode_base_url} /search-in-rotated-sorted-array/[33. 搜索旋转排序数组^]
165+ |{doc_base_url} /0033-search-in-rotated-sorted-array.adoc[题解]
166+ |⭕️ 重点关注有序部分,优先在有序部分查找,不在有序部分再去无序部分查找。注意判定有序部分的方法: `nums[0]` 与 `nums[mid]` 相比较,而不是 `nums[left]` (它的值会来回变)。
167+
163168|===
164169
165170截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line number Diff line number Diff line change 1+ package com .diguage .algo .leetcode ;
2+
3+ public class _0033_SearchInRotatedSortedArray_3 {
4+ // tag::answer[]
5+
6+ /**
7+ * @author D瓜哥 · https://www.diguage.com
8+ * @since 2025-04-06 14:59:46
9+ */
10+ public int search (int [] nums , int target ) {
11+ int left = 0 , right = nums .length - 1 ;
12+ while (left <= right ) {
13+ int mid = left + (right - left ) / 2 ;
14+ if (nums [mid ] == target ) {
15+ return mid ;
16+ }
17+ if (nums [0 ] <= nums [mid ]) {
18+ if (nums [left ] <= target && target < nums [mid ]) {
19+ right = mid - 1 ;
20+ } else {
21+ left = mid + 1 ;
22+ }
23+ } else {
24+ if (nums [mid ] < target && target <= nums [right ]) {
25+ left = mid + 1 ;
26+ } else {
27+ right = mid - 1 ;
28+ }
29+ }
30+ }
31+ return -1 ;
32+ }
33+ // end::answer[]
34+ }
You can’t perform that action at this time.
0 commit comments