Skip to content

Commit

Permalink
adding solution for 704. Binary search | golang
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexS778 committed Aug 7, 2022
1 parent 5d5abf7 commit 69ebcea
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions go/704-Binary-Search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

func search(nums []int, target int) int {
left := 0
right := len(nums) - 1

for left <= right {
middle := (left + right) / 2

if nums[middle] == target {
return middle
} else if nums[middle] < target {
left = middle + 1
} else {
right = middle - 1
}
}
return -1

}

0 comments on commit 69ebcea

Please sign in to comment.