577.Find the First Occurrence of a Value
Easy
Given an array
nums (which may contain repeated values) and a target, return the index of the target's first (leftmost) occurrence, or -1 if it does not appear.
With duplicates, "where is it?" has several answers, so the question must say which one. The scan directionScan DirectionWhether a loop walks from the first index up or from the last index down — it decides which match is met first. decides which match is seen first: scanning from the left finds the first occurrence immediately, while scanning from the right has to go all the way through.
Example 1:
Input: nums = [4,9,2,9,9], target = 9
Output: 1
Example 2:
Input: nums = [4,9,2], target = 7
Output: -1
+ 4 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 1000 - ●
-1000000 ≤ nums[i], target ≤ 1000000
nums =
[4, 9, 2, 9, 9]
target =
9