Closest Number in Sorted Array
Given a target number and an integer array A sorted in ascending order, find the indexi
in A such that A[i] is closest to the given target.
Return -1 if there is no element in the array.
Example
Given[1, 2, 3]
and target =2
, return1
.
Given[1, 4, 6]
and target =3
, return1
.
Given[1, 4, 6]
and target =5
, return1
or2
.
Given[1, 3, 3, 4]
and target =2
, return0
or1
or2
.
分析:
主要注意一下最后选择start还是end的事就行
Last updated
Was this helpful?