-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch_in_Rotated_Sorted_Array.java
More file actions
162 lines (114 loc) · 3.46 KB
/
Copy pathSearch_in_Rotated_Sorted_Array.java
File metadata and controls
162 lines (114 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
There is an integer array nums sorted in ascending order (with distinct values).
Prior to being passed to your function, nums is possibly left rotated at an unknown index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be left rotated by 3 indices and become [4,5,6,7,0,1,2].
Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
You must write an algorithm with O(log n) runtime complexity.
Example 1:
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1
Example 3:
Input: nums = [1], target = 0
Output: -1
Constraints:
1 <= nums.length <= 5000
-104 <= nums[i] <= 104
All values of nums are unique.
nums is an ascending array that is possibly rotated.
-104 <= target <= 104
*/
class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
int low = 0;
int high = n - 1;
while (low <= high) {
int guess = (low + high) / 2;
if (nums[guess] == target) {
return guess;
}
if (nums[guess] > nums[n - 1]) {
if (nums[guess] < target) {
low = guess + 1;
}
else {
if (nums[0] > target) {
low = guess + 1;
}
else {
high = guess - 1;
}
}
}
else {
if (nums[guess] > target) {
high = guess - 1;
}
else {
if (nums[n - 1] < target) {
high = guess - 1;
}
else {
low = guess + 1;
}
}
}
}
return -1;
}
}
/*
Method 2 (Using Binary Search twice)
class Search_in_Rotated_Sorted_Array {
public int search(int[] nums, int target) {
int n = nums.length;
int res = -1;
int low = 0;
int high = n - 1;
int pivot = 0;
while (low <= high) {
int guess = (low + high) / 2;
if (nums[guess] > nums[n - 1]) {
low = guess + 1;
}
else {
pivot = guess;
high = guess - 1;
}
}
int low1 = 0;
int high1 = pivot - 1;
int low2 = pivot;
int high2 = n - 1;
while (low1 <= high1) {
int guess1 = (low1 + high1) / 2;
if (nums[guess1] > target) {
high1 = guess1 - 1;
}
else if (nums[guess1] < target) {
low1 = guess1 + 1;
}
else {
res = guess1;
return res;
}
}
while (low2 <= high2) {
int guess2 = (low2 + high2) / 2;
if (nums[guess2] > target) {
high2 = guess2 - 1;
}
else if (nums[guess2] < target) {
low2 = guess2 + 1;
}
else {
res = guess2;
return res;
}
}
return res;
}
}
*/