Skip to content

Commit

Permalink
init: Binary Search
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed May 17, 2024
1 parent 3295c6c commit 2da476b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions leetcode/BinarySearch/704.BinarySearch/sol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// https://leetcode.com/problems/binary-search

package main

import (
"fmt"
)

func search(nums []int, target int) int {
left, right := 0, len(nums)-1
for left < right {
mid := (left + right) >> 1

if nums[mid] >= target {
right = mid
} else {
left = mid + 1
}
}
if nums[left] == target {
return left
}

return -1
}

func main() {
nums := []int{-1, 0, 3, 5, 9, 12}
target := 9

fmt.Println(search(nums, target))
}

0 comments on commit 2da476b

Please sign in to comment.