Skip to content

Commit

Permalink
Add scala solution for 33-Search-in-Rotated-Sorted-Array
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisKheng committed Oct 1, 2022
1 parent 8507558 commit 19337b9
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions scala/33-Search-in-Rotated-Sorted-Array.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
object Solution {
def search(nums: Array[Int], target: Int): Int = {
var (left, right) = (0, nums.length - 1)

while (left <= right) {
val mid = (left + right) / 2

if (target == nums(mid)) {
return mid
} else if (nums(mid) >= nums(left)) {
if (target >= nums(left) && target < nums(mid)) {
right = mid - 1
} else {
left = mid + 1
}
} else {
if (target > nums(mid) && target <= nums(right)) {
left = mid + 1
} else {
right = mid - 1
}
}
}

return -1
}
}

0 comments on commit 19337b9

Please sign in to comment.