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 ffbf62c commit 796b845Copy full SHA for 796b845
0033-search-in-rotated-sorted-array/0033-search-in-rotated-sorted-array.java
@@ -0,0 +1,22 @@
1
+// search the number in the rotated sorted array; solve this in n(logn) time
2
+
3
+// method: binary search-left&right pointer
4
5
+class Solution {
6
+ public int search(int[] nums, int target) {
7
+ int start = 0, end = nums.length - 1;
8
+ while (start <= end) {
9
+ int mid = start + (end - start) / 2;
10
+ if (nums[mid] == target) return mid;
11
+ else if (nums[mid] >= nums[start]) {
12
+ if (target >= nums[start] && target < nums[mid]) end = mid - 1;
13
+ else start = mid + 1;
14
+ }
15
+ else {
16
+ if (target <= nums[end] && target > nums[mid]) start = mid + 1;
17
+ else end = mid - 1;
18
19
20
+ return -1;
21
22
+}
0 commit comments