Skip to content

Commit

Permalink
feat: LC-0033: Search in Rotated Sorted Array
Browse files Browse the repository at this point in the history
add java solution for leetcode problem 33 - Search in Rotated Sorted Array
  • Loading branch information
galaumang committed Oct 1, 2023
1 parent 5907848 commit 67ce3f7
Showing 1 changed file with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dsalgo.array;

public class LC0033_SearchRotatedSortedArray {
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;

while (left <= right) {
int mid = left + (right - left) / 2;
if (target == nums[mid])
return mid;
if (nums[left] <= nums[mid]) {
if (nums[left] <= target && target < nums[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
if (nums[mid] < target && target <= nums[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return -1;
}
}

0 comments on commit 67ce3f7

Please sign in to comment.