Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e4f5e91

Browse files
Add Solution2.java to problems 0033
1 parent b9be8a4 commit e4f5e91

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /