1920. Build Array from Permutation

problem

solution

option 1

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
vector<int> buildArray(vector<int>& nums) {
int n= nums.size();
vector<int> ret;
for(int i=0;i<n;++i){
ret.push_back(nums[nums[i]]);
}
return ret;
}
};

option 2 - follow up O(1) space

analysis

  • time complexity O(n)
  • space complexity O(n)