LeetCode1122:数组的相对排序

该博客介绍了如何使用C++解决一个排序问题,即根据给定的数组arr2对数组arr1进行排序,保持arr1中元素的相对顺序,并将未在arr2中出现的元素按升序排列。文中提供了三种不同的解法,包括自定义排序、元组排序和一般解法,每种解法都利用了unordered_map和排序函数。
摘要由CSDN通过智能技术生成

题目描述

给你两个数组,arr1 和 arr2,

arr2 中的元素各不相同
arr2 中的每个元素都出现在 arr1 中
对 arr1 中的元素进行排序,使 arr1 中项的相对顺序和 arr2 中的相对顺序相同。未在 arr2 中出现过的元素需要按照升序放在 arr1 的末尾。

示例:

输入:arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
输出:[2,2,2,1,4,3,3,9,6,7,19]

提示:

1 <= arr1.length, arr2.length <= 1000
0 <= arr1[i], arr2[i] <= 1000
arr2 中的元素 arr2[i] 各不相同
arr2 中的每个元素 arr2[i] 都出现在 arr1 中

解法一:自定义排序

在这里插入图片描述

class Solution {
public:
    vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
        unordered_map<int, int> rank;
        for (int i = 0; i < arr2.size(); ++i) {
            rank[arr2[i]] = i;
        }
        sort(arr1.begin(), arr1.end(), [&](int x, int y) {
            if (rank.count(x)) {
                return rank.count(y) ? rank[x] < rank[y] : true;
            }
            else {
                return rank.count(y) ? false : x < y;
            }
        });
        return arr1;
    }
};

解法二:元组排序

class Solution {
public:
    vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
        unordered_map<int, int> rank;
        for (int i = 0; i < arr2.size(); i++) {
            rank[arr2[i]] = i; // rank[x]表示数组arr2中x对应的位置索引
        }
        
        auto mycmp = [&](int x) -> pair<int, int> { // x在arr2中返回[0, ran[x]]; x不在arr2中, 返回[1,x]
            return rank.count(x) ? make_pair(0, rank[x] ): make_pair(1, x);
        };

        sort(arr1.begin(), arr1.end(), [&](int x, int y) {
            return mycmp(x) < mycmp(y);
        });

        return arr1;
    }
};

进一步优化
在这里插入图片描述

class Solution {
public:
    vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
        unordered_map<int, int> rank;
        int n = arr2.size();
        for (int i = 0; i < n; ++i) {
            rank[arr2[i]] = i - n;
        }
        sort(arr1.begin(), arr1.end(), [&](int x, int y) {
            return (rank.count(x) ? rank[x] : x) < (rank.count(y) ? rank[y] : y);
        });
        return arr1;
    }
};

c++ 17 结构化绑定用法对mycmp的用法:

auto mycmp = [&](int x) -> pair<int, int> { // x在arr2中返回[0, x]; x不在arr2中, 返回[1,x]
            return rank.count(x) ? {0, rank[x]} : {1, x};
        };

另外一种表达:

auto mycmp = [&](int x) -> pair<int, int> { // x在arr2中返回[0, x]; x不在arr2中, 返回[1,x]
            return rank.count(x) ? pair{0, rank[x]} : pair{1, x};
        };

在这里插入图片描述

解法三:一般解法

class Solution {
public:
   vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
      vector<int> counts(1001, 0);
      vector<int> res1;
      vector<int> res2;
      for (auto& i : arr1) { // 统计arr1中每个元素出现的次数
         counts[i]++;
      }

      for (auto& n:arr2) {
         while (counts[n] > 0) { // 对arr1中包含arr2的所有元素进行排序
            res1.push_back(n);
            counts[n]--;
         }
      }

      for (int i = 0; i < counts.size(); i++) {
         while (counts[i] > 0) { // 对arr1中不包含arr2的所有元素进行排序
            res2.push_back(i);
            counts[i]--;
         }
      }
      sort(res2.begin(),res2.end());
      res1.insert(res1.end(),res2.begin(),res2.end());
      return res1;
   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值