Explanation

578.Find the Last Occurrence of a Value

Easy
Given an array nums (which may contain repeated values) and a target, return the index of the target's last (rightmost) occurrence, or -1 if it does not appear. This is the mirror image of finding the first occurrence. Scanning from the left, you only know a match is the last one after reaching the end; reversing the scanReverse IterationLooping from the last index down to the first, e.g. for (i = n − 1; i >= 0; i--). lets the search stop at the first match it meets.

Example 1:

Input: nums = [4,9,2,9,1], target = 9

Output: 3

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, 1]

target =

9