返回介绍

solution / 2100-2199 / 2191.Sort the Jumbled Numbers / README_EN

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

2191. Sort the Jumbled Numbers

中文文档

Description

You are given a 0-indexed integer array mapping which represents the mapping rule of a shuffled decimal system. mapping[i] = j means digit i should be mapped to digit j in this system.

The mapped value of an integer is the new integer obtained by replacing each occurrence of digit i in the integer with mapping[i] for all 0 <= i <= 9.

You are also given another integer array nums. Return _the array _nums_ sorted in non-decreasing order based on the mapped values of its elements._

Notes:

  • Elements with the same mapped values should appear in the same relative order as in the input.
  • The elements of nums should only be sorted based on their mapped values and not be replaced by them.

 

Example 1:

Input: mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38]
Output: [338,38,991]
Explanation: 
Map the number 991 as follows:
1. mapping[9] = 6, so all occurrences of the digit 9 will become 6.
2. mapping[1] = 9, so all occurrences of the digit 1 will become 9.
Therefore, the mapped value of 991 is 669.
338 maps to 007, or 7 after removing the leading zeros.
38 maps to 07, which is also 7 after removing leading zeros.
Since 338 and 38 share the same mapped value, they should remain in the same relative order, so 338 comes before 38.
Thus, the sorted array is [338,38,991].

Example 2:

Input: mapping = [0,1,2,3,4,5,6,7,8,9], nums = [789,456,123]
Output: [123,456,789]
Explanation: 789 maps to 789, 456 maps to 456, and 123 maps to 123. Thus, the sorted array is [123,456,789].

 

Constraints:

  • mapping.length == 10
  • 0 <= mapping[i] <= 9
  • All the values of mapping[i] are unique.
  • 1 <= nums.length <= 3 * 104
  • 0 <= nums[i] < 109

Solutions

Solution 1

class Solution:
  def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:
    arr = []
    for i, x in enumerate(nums):
      y = mapping[0] if x == 0 else 0
      k = 1
      while x:
        x, v = divmod(x, 10)
        y = mapping[v] * k + y
        k *= 10
      arr.append((y, i))
    arr.sort()
    return [nums[i] for _, i in arr]
class Solution {
  public int[] sortJumbled(int[] mapping, int[] nums) {
    int n = nums.length;
    int[][] arr = new int[n][2];
    for (int i = 0; i < n; ++i) {
      int x = nums[i];
      int y = x == 0 ? mapping[0] : 0;
      int k = 1;
      for (; x > 0; x /= 10) {
        y += k * mapping[x % 10];
        k *= 10;
      }
      arr[i] = new int[] {y, i};
    }
    Arrays.sort(arr, (a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
    int[] ans = new int[n];
    for (int i = 0; i < n; ++i) {
      ans[i] = nums[arr[i][1]];
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> sortJumbled(vector<int>& mapping, vector<int>& nums) {
    int n = nums.size();
    vector<pair<int, int>> arr(n);
    for (int i = 0; i < n; ++i) {
      int x = nums[i];
      int y = x == 0 ? mapping[0] : 0;
      int k = 1;
      for (; x; x /= 10) {
        y += k * mapping[x % 10];
        k *= 10;
      }
      arr[i] = {y, i};
    }
    sort(arr.begin(), arr.end());
    vector<int> ans;
    for (auto& [_, i] : arr) {
      ans.push_back(nums[i]);
    }
    return ans;
  }
};
func sortJumbled(mapping []int, nums []int) (ans []int) {
  n := len(nums)
  arr := make([][2]int, n)
  for i, x := range nums {
    y := 0
    if x == 0 {
      y = mapping[0]
    }
    k := 1
    for ; x > 0; x /= 10 {
      y += k * mapping[x%10]
      k *= 10
    }
    arr[i] = [2]int{y, i}
  }
  sort.Slice(arr, func(i, j int) bool {
    a, b := arr[i], arr[j]
    return a[0] < b[0] || a[0] == b[0] && a[1] < b[1]
  })
  for _, x := range arr {
    ans = append(ans, nums[x[1]])
  }
  return
}
function sortJumbled(mapping: number[], nums: number[]): number[] {
  const n = nums.length;
  const arr: number[][] = [];
  for (let i = 0; i < n; ++i) {
    let x = nums[i];
    let y = x === 0 ? mapping[0] : 0;
    let k = 1;
    for (; x > 0; x = Math.floor(x / 10), k *= 10) {
      y += mapping[x % 10] * k;
    }
    arr.push([y, i]);
  }
  arr.sort((a, b) => (a[0] === b[0] ? a[1] - b[1] : a[0] - b[0]));
  return arr.map(a => nums[a[1]]);
}

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

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

发布评论

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