745. Return All Indices of Target in Array

EasyRecursionRecursion

Given an integer array nums and an integer target, return an array containing ALL indices at which target appears in nums, in increasing order. If target does not appear, return an empty array. You must collect the indices recursively, without using a loop.

Input: An integer array nums and an integer target.

Output: Return an array of indices as [i1,i2,...] (or [] if none).

Examples

Example 1
Input: [1,2,3,2,1], 2
Output: [1,3]
Explanation: 2 occurs at indices 1 and 3.
Example 2
Input: [5,5,5,5], 5
Output: [0,1,2,3]
Explanation: Every position matches.
Example 3
Input: [1,2,3], 4
Output: []
Explanation: 4 never appears.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →