File tree 1 file changed +28
-0
lines changed
solution/0033.Search in Rotated Sorted Array
1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int search (int [] nums , int target ) {
3
+ return solution (nums , target , 0 , nums .length - 1 );
4
+ }
5
+
6
+ private int solution (int [] nums , int target , int left , int right ) {
7
+ while (left <= right ) {
8
+ int mid = left + (right - left ) / 2 ;
9
+ if (target == nums [mid ]) {
10
+ return mid ;
11
+ }
12
+ // rotation point is to the right && target value is also on the right
13
+ if (nums [mid ] > nums [right ] && (target < nums [left ] || target > nums [mid ])) {
14
+ return solution (nums , target , mid + 1 , right );
15
+ }
16
+ // rotation point is to the left && target value is also on the left
17
+ if (nums [mid ] < nums [left ] && (target < nums [mid ] || target > nums [right ])) {
18
+ return solution (nums , target , left , mid - 1 );
19
+ }
20
+ if (target > nums [mid ]) {
21
+ left = mid + 1 ;
22
+ } else {
23
+ right = mid - 1 ;
24
+ }
25
+ }
26
+ return -1 ;
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments