Problem - Leetcode
Given an array of integers nums
which is sorted in ascending order, and an integer target
, write a function to search target
in nums
. If target
exists, then return its index. Otherwise, return -1
.
You must write an algorithm with O(log n)
runtime complexity.
Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
Constraints:
1 <= nums.length <= 10<sup>4</sup>
-10<sup>4</sup> < nums[i], target < 10<sup>4</sup>
All the integers in
nums
are unique.nums
is sorted in ascending order.30 <= temperatures[i] <= 100
Solution in Golang
func search(nums []int, target int) int {
left := 0
right := len(nums)-1
for left<=right{
mid := (left+right)/2
if nums[mid]==target{
return mid
}else if nums[mid]<target{
left = mid +1
}else {
right = mid -1
}
}
return -1
}
This is a Go programming language function called search
that performs a binary search to find a target integer within a sorted array of integers. Let me break down the code step by step:
func search(nums []int, target int) int {
: This line defines a function namedsearch
that takes two arguments:nums
is a slice of integers, representing the sorted array in which we want to search for thetarget
value.target
is the integer we want to find in thenums
array.The function returns an integer, which is the index of the
target
in the array if found, or -1 if it's not found.
left := 0
andright := len(nums)-1
: These lines initialize two variablesleft
andright
.left
represents the left boundary of the current search range, initialized to the beginning of the array (index 0), andright
represents the right boundary, initialized to the end of the array (indexlen(nums)-1
).for left <= right {
: This starts a loop that continues as long as theleft
boundary is less than or equal to theright
boundary. This loop is the heart of the binary search algorithm.mid := (left+right)/2
: Inside the loop, it calculates the middle indexmid
of the current search range by taking the average ofleft
andright
. This is where the binary search gets its name because it repeatedly divides the search range in half.if nums[mid] == target {
: This checks if the value at the middle indexmid
of the arraynums
is equal to thetarget
. If it is, this means we've found thetarget
, and the function returnsmid
, which is the index wheretarget
is located in the array.else if nums[mid] < target {
: If the value atmid
is less than thetarget
, which means thetarget
must be in the right half of the current search range. So, it updatesleft
tomid + 1
, effectively narrowing the search range to the right half.else {
: If the value atmid
is greater than thetarget
, which means thetarget
must be in the left half of the current search range. So, it updatesright
tomid - 1
, narrowing the search range to the left half.If the loop exits without finding the
target
, i.e.,left
becomes greater thanright
, the function returns-1
to indicate that thetarget
is not in the array.
In summary, this code performs an efficient binary search on a sorted array to find the index of a specific target value, and it returns that index or -1 if the target is not in the array. Binary search is efficient because it repeatedly divides the search range in half, reducing the number of comparisons needed to find the target in a sorted array.