返回介绍

solution / 2200-2299 / 2295.Replace Elements in an Array / README_EN

发布于 2024-06-17 01:03:07 字数 4572 浏览 0 评论 0 收藏 0

2295. Replace Elements in an Array

中文文档

Description

You are given a 0-indexed array nums that consists of n distinct positive integers. Apply m operations to this array, where in the ith operation you replace the number operations[i][0] with operations[i][1].

It is guaranteed that in the ith operation:

  • operations[i][0] exists in nums.
  • operations[i][1] does not exist in nums.

Return _the array obtained after applying all the operations_.

 

Example 1:

Input: nums = [1,2,4,6], operations = [[1,3],[4,7],[6,1]]
Output: [3,2,7,1]
Explanation: We perform the following operations on nums:
- Replace the number 1 with 3. nums becomes [3,2,4,6].
- Replace the number 4 with 7. nums becomes [3,2,7,6].
- Replace the number 6 with 1. nums becomes [3,2,7,1].
We return the final array [3,2,7,1].

Example 2:

Input: nums = [1,2], operations = [[1,3],[2,1],[3,2]]
Output: [2,1]
Explanation: We perform the following operations to nums:
- Replace the number 1 with 3. nums becomes [3,2].
- Replace the number 2 with 1. nums becomes [3,1].
- Replace the number 3 with 2. nums becomes [2,1].
We return the array [2,1].

 

Constraints:

  • n == nums.length
  • m == operations.length
  • 1 <= n, m <= 105
  • All the values of nums are distinct.
  • operations[i].length == 2
  • 1 <= nums[i], operations[i][0], operations[i][1] <= 106
  • operations[i][0] will exist in nums when applying the ith operation.
  • operations[i][1] will not exist in nums when applying the ith operation.

Solutions

Solution 1

class Solution:
  def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:
    d = {v: i for i, v in enumerate(nums)}
    for a, b in operations:
      nums[d[a]] = b
      d[b] = d[a]
    return nums
class Solution {
  public int[] arrayChange(int[] nums, int[][] operations) {
    Map<Integer, Integer> d = new HashMap<>();
    for (int i = 0; i < nums.length; ++i) {
      d.put(nums[i], i);
    }
    for (var op : operations) {
      int a = op[0], b = op[1];
      nums[d.get(a)] = b;
      d.put(b, d.get(a));
    }
    return nums;
  }
}
class Solution {
public:
  vector<int> arrayChange(vector<int>& nums, vector<vector<int>>& operations) {
    unordered_map<int, int> d;
    for (int i = 0; i < nums.size(); ++i) {
      d[nums[i]] = i;
    }
    for (auto& op : operations) {
      int a = op[0], b = op[1];
      nums[d[a]] = b;
      d[b] = d[a];
    }
    return nums;
  }
};
func arrayChange(nums []int, operations [][]int) []int {
  d := map[int]int{}
  for i, v := range nums {
    d[v] = i
  }
  for _, op := range operations {
    a, b := op[0], op[1]
    nums[d[a]] = b
    d[b] = d[a]
  }
  return nums
}
function arrayChange(nums: number[], operations: number[][]): number[] {
  const d = new Map(nums.map((v, i) => [v, i]));
  for (const [a, b] of operations) {
    nums[d.get(a)] = b;
    d.set(b, d.get(a));
  }
  return nums;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文