LeetCode 题解工作台

将杂乱无章的数字排序

给你一个下标从 0 开始的整数数组 mapping ,它表示一个十进制数的映射规则, mapping[i] = j 表示这个规则下将数位 i 映射为数位 j 。 一个整数 映射后的值 为将原数字每一个数位 i ( 0 )映射为 mapping[i] 。 另外给你一个整数数组 nums ,请你将数组 …

category

2

题型

code_blocks

8

代码语言

hub

3

相关题

当前训练重点

中等 · 数组·排序

bolt

答案摘要

我们遍历数组 中的每个元素 ,将其映射后的值 与下标 一起存入数组 中,然后对数组 进行排序,最后将排序后的数组 中的下标 取出,转换为原数组 中的元素 即可。 时间复杂度 $O(n \times \log n)$,空间复杂度 。其中 为数组 的长度。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数组·排序 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个下标从 0 开始的整数数组 mapping ,它表示一个十进制数的映射规则,mapping[i] = j 表示这个规则下将数位 i 映射为数位 j 。

一个整数 映射后的值 为将原数字每一个数位 i (0 <= i <= 9)映射为 mapping[i] 。

另外给你一个整数数组 nums ,请你将数组 nums 中每个数按照它们映射后对应数字非递减顺序排序后返回。

注意:

  • 如果两个数字映射后对应的数字大小相同,则将它们按照输入中的 相对顺序 排序。
  • nums 中的元素只有在排序的时候需要按照映射后的值进行比较,返回的值应该是输入的元素本身。

 

示例 1:

输入:mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38]
输出:[338,38,991]
解释:
将数字 991 按照如下规则映射:
1. mapping[9] = 6 ,所有数位 9 都会变成 6 。
2. mapping[1] = 9 ,所有数位 1 都会变成 9 。
所以,991 映射的值为 669 。
338 映射为 007 ,去掉前导 0 后得到 7 。
38 映射为 07 ,去掉前导 0 后得到 7 。
由于 338 和 38 映射后的值相同,所以它们的前后顺序保留原数组中的相对位置关系,338 在 38 的前面。
所以,排序后的数组为 [338,38,991] 。

示例 2:

输入:mapping = [0,1,2,3,4,5,6,7,8,9], nums = [789,456,123]
输出:[123,456,789]
解释:789 映射为 789 ,456 映射为 456 ,123 映射为 123 。所以排序后数组为 [123,456,789] 。

 

提示:

  • mapping.length == 10
  • 0 <= mapping[i] <= 9
  • mapping[i] 的值 互不相同 。
  • 1 <= nums.length <= 3 * 104
  • 0 <= nums[i] < 109
lightbulb

解题思路

方法一:自定义排序

我们遍历数组 numsnums 中的每个元素 nums[i]nums[i],将其映射后的值 yy 与下标 ii 一起存入数组 arrarr 中,然后对数组 arrarr 进行排序,最后将排序后的数组 arrarr 中的下标 ii 取出,转换为原数组 numsnums 中的元素 nums[i]nums[i] 即可。

时间复杂度 O(n×logn)O(n \times \log n),空间复杂度 O(n)O(n)。其中 nn 为数组 numsnums 的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:
        def f(x: int) -> int:
            if x == 0:
                return mapping[0]
            y, k = 0, 1
            while x:
                x, v = divmod(x, 10)
                v = mapping[v]
                y = k * v + y
                k *= 10
            return y

        arr = sorted((f(x), i) for i, x in enumerate(nums))
        return [nums[i] for _, i in arr]
speed

复杂度分析

指标
时间O(n\cdot \log n)
空间O(n)
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for candidates who understand how mapping and sorting can be combined for a custom sorting problem.

  • question_mark

    Candidates should demonstrate understanding of stability in sorting, ensuring that equal mapped values preserve their relative order.

  • question_mark

    Expect candidates to efficiently handle mapping transformations and sorting of numbers in non-decreasing order.

warning

常见陷阱

外企场景
  • error

    Misunderstanding the mapping transformation and mistakenly modifying the original numbers without mapping their digits first.

  • error

    Not maintaining the relative order of numbers when their mapped values are the same, causing incorrect sorting results.

  • error

    Inefficient mapping and sorting logic, leading to higher time complexity than the expected O(n log n).

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Increasing the length of the nums array to test performance under larger inputs.

  • arrow_right_alt

    Adjusting the mapping to include digits in reverse order or with repeated values, affecting the mapping logic.

  • arrow_right_alt

    Providing edge cases where nums contains a single element or multiple elements with identical mapped values.

help

常见问题

外企场景

将杂乱无章的数字排序题解:数组·排序 | LeetCode #2191 中等