1920. Build Array from Permutation

Solution

class Solution {
public:
    vector<int> buildArray(vector<int>& nums) {
        vector<int> ans(nums.size());
        
        for (int i = 0; i < nums.size(); i++) {
            ans[i] = nums[nums[i]];
        }
        
        return ans;
    }
};

Complexity

  • Time Complexity: O(N), where N is the size of nums.
  • Space Complexity: O(N), where N is the size of nums.

All Solutions