-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: LC-0033: Search in Rotated Sorted Array
add java solution for leetcode problem 33 - Search in Rotated Sorted Array
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
solutions-java/src/main/java/dsalgo/array/LC0033_SearchRotatedSortedArray.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |