Skip to content

Commit

Permalink
Create: 33-Search-in-Rotated-Sorted-Array.go
Browse files Browse the repository at this point in the history
  • Loading branch information
Ykhan799 authored Sep 13, 2022
1 parent 28929d6 commit f6eb981
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions go/33-Search-in-Rotated-Sorted-Array.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
func search(nums []int, target int) int {
left, right := 0, len(nums) - 1

for left <= right {
mid := (left + right) / 2
if target == nums[mid] {
return mid
}

// left sorted portion
if nums[left] <= nums[mid] {
if target > nums[mid] || target < nums[left] {
left = mid + 1
} else {
right = mid - 1
}
// Right sorted portion
} else {
if target < nums[mid] || target > nums[right] {
right = mid - 1
} else {
left = mid + 1
}
}
}
return -1
}

0 comments on commit f6eb981

Please sign in to comment.