← 返回 bytedance 的题目列表Binary Search on a Sorted Array (Find Target)
类型:online_judge
Given a sorted ascending array nums (possibly empty) and an integer target, find the index of target in the array.
If target exists, output its 0-based index.
If target does not exist, output -1.
Requirement: O(log n) time.
Input
Line 1: integer n, the length of the array.
Line 2: n integers, the ascending array nums.
Line 3: integer target.
Output
One integer: the index or -1.
Constraints
0 <= n <= 2*10^5
-10^9 <= nums[i], target <= 10^9
nums is strictly increasing (no duplicates)
Example
Input:
5
1 3 5 7 9
7
Output:
3
Example
Input
5
1 3 5 7 9
7
Output
3