We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b9be8a4 commit e4f5e91Copy full SHA for e4f5e91
solution/0033.Search in Rotated Sorted Array/Solution2.java
@@ -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
+}
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments