Given an integer array nums where nums[i] is either a positive integer or -1. We need to find for each -1 the respective positive integer, which we call the last visited integer.
To achieve this goal, let's define two empty arrays: seen and ans.
Start iterating from the beginning of the array nums.
Return the array ans.
Examples
Example 1
Input: nums = [1,2,-1,-1,-1]
Output: [2,1,-1]
Explanation:
Start with seen = [] and ans = [] .
Example 2
Input: nums = [1,-1,2,-1,-1]
Output: [1,2,1]
Explanation:
Start with seen = [] and ans = [] .
Constraints
1 <= nums.length <= 100
nums[i] == -1 or 1 <= nums[i] <= 100
2899. Last Visited Integers
Easy
10 Points
Array
Simulation
Given an integer array nums where nums[i] is either a positive integer or -1. We need to find for each -1 the respective positive integer, which we call the last visited integer.
To achieve this goal, let's define two empty arrays: seen and ans.
Start iterating from the beginning of the array nums.
Return the array ans.
Examples
Example 1
Input: nums = [1,2,-1,-1,-1]
Output: [2,1,-1]
Explanation:
Start with seen = [] and ans = [] .
Example 2
Input: nums = [1,-1,2,-1,-1]
Output: [1,2,1]
Explanation:
Start with seen = [] and ans = [] .
Constraints
1 <= nums.length <= 100
nums[i] == -1 or 1 <= nums[i] <= 100
Last Visited Integers - Practice Coding | SlaveCode