← TopicsBinary Search · 1/21
Binary search — find index
Return the index of target in the ascending-sorted nums, or -1 if absent.
func search(nums []int, target int) int {mid := (lo + hi) / 2if nums[mid] == target {return mid} else if nums[mid] < target {lo = mid + 1} else {hi = mid - 1}}return -1}
this session — 0 attempt(s), 0 passed, 0 failed. (ephemeral; discarded when you leave)