-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReplaceElementsInAnArray.cpp
34 lines (26 loc) · 1.01 KB
/
ReplaceElementsInAnArray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
public:
vector<int> arrayChange(vector<int>& nums, vector<vector<int>>& operations) {
map<int, int> dict;
for (int i = 0; i < operations.size(); i++) {
map<int, int>::iterator it = dict.find(operations[i][0]);
if (it != dict.end()) {
dict[operations[i][1]] = dict[operations[i][0]];
dict.erase(operations[i][0]);
} else {
dict[operations[i][1]] = operations[i][0];
}
}
map<int, int> dict_reversed;
for (map<int,int>::iterator i = dict.begin(); i != dict.end(); ++i) {
dict_reversed[i->second] = i->first;
}
for (int i = 0; i < nums.size(); i++) {
map<int, int>::iterator it = dict_reversed.find(nums[i]);
if (it != dict_reversed.end()) {
nums[i] = dict_reversed[nums[i]];
}
}
return nums;
}
};