描述
给你一个下标从 0 开始的数组 nums ,它包含 n 个 互不相同 的正整数。请你对这个数组执行 m 个操作,在第 i 个操作中,你需要将数字 operations[i][0] 替换成 operations[i][1] 。
题目保证在第 i 个操作中:
operations[i][0] 在 nums 中存在。
operations[i][1] 在 nums 中不存在。
请你返回执行完所有操作后的数组。
分析
使用哈希表存储nums的元素,值是nums的下标。
使用哈希可以快速定位想要操作的数,比遍历nums的效率要高。
class Solution {
public int[] arrayChange(int[] nums, int[][] operations) {
Map<Integer,Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i],i);
}
for (int[] arr : operations) {
int index = map.get(arr[0]);
map.put(arr[1],index);
map.remove(arr[0]);
}
for (int num : map.keySet()) {
nums[map.get(num)] = num;
}
return nums;
}
}