Relative Sort Array

Relative Sort Array

Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.

Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2.  Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order.

Example 1:

Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
Output: [2,2,2,1,4,3,3,9,6,7,19]

Constraints:

arr1.length, arr2.length <= 1000
0 <= arr1[i], arr2[i] <= 1000
Each arr2[i] is distinct.
Each arr2[i] is in arr1.

Code

//
//  main.cpp
//  按照字符串2对字符串1进行排序
//
//  Created by mac on 2019/7/20.
//  Copyright ? 2019 mac. All rights reserved.
//

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    vector<int> relativeSortArray(vector<int>& arr1, vector<int>& arr2) {
        vector<int> arr3,arr4;
        for (int i=0;i<arr2.size() ; ++i) {
            for (int j=0; j<arr1.size(); ++j) {
                if (arr1[j]==arr2[i]) {
                    arr3.push_back(arr2[i]);
                    arr1[j]=1001;
                }
            }
        }
        vector<int>::iterator it=arr1.begin();
        while (it!=arr1.end()) {
            if (*it!=1001) {
                arr4.push_back(*it);
            }
            it++;
        }
        sort(arr4.begin(), arr4.end());
        for (int j=0; j<arr4.size(); ++j) {
            arr3.push_back(arr4[j]);
        }
        return arr3;
    }
};

int main(int argc, const char * argv[]) {
    // insert code here...
    vector<int> arr1,arr2,arr;
    arr1={2,3,1,3,2,4,6,7,9,2,19};
    arr2={2,1,4,3,9,6};
    Solution so;
    arr=so.relativeSortArray(arr1, arr2);
    for (int i=0; i<arr.size(); ++i) {
        cout<<arr[i]<<" ";
    }

    cout<<endl;
//    [2,3,1,3,2,4,6,7,9,2,19]
//    [2,1,4,3,9,6]

    return 0;
}

运行结果

2 2 2 1 4 3 3 9 6 7 19
Program ended with exit code: 0

参考文献

原文地址:https://www.cnblogs.com/overlows/p/11219563.html

时间: 2024-11-02 03:35:08

Relative Sort Array的相关文章

LeetCode 1122. Relative Sort Array (数组的相对排序)

题目标签:Sort 先设立一个 size 1000 的 array cnt [], 把arr1 里面的数字 计数存入 cnt: 遍历 arr2, 按照arr2 的顺序把 arr1 与 arr2 重复的数字 先存入 arr1: 遍历 cnt,把 cnt 里面剩余的 arr1 剩下的数字 存入arr1: 具体看code.  Java Solution: Runtime:  0 ms, faster than 100.00 % Memory Usage: 38 MB, less than 100.00

【Golang语言】LeetCode1122. Relative Sort Array

给你两个数组,arr1 和?arr2, arr2?中的元素各不相同arr2 中的每个元素都出现在?arr1?中对 arr1?中的元素进行排序,使 arr1 中项的相对顺序和?arr2?中的相对顺序相同.未在?arr2?中出现过的元素需要按照升序放在?arr1?的末尾.https://leetcode-cn.com/problems/relative-sort-array 输入:arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6] 输出:[2,

[leetcode][easy][Array][905][Sort Array By Parity]

Sort Array By Parity 题目链接 题目描述 给定一个非负整型数组A,返回一个数组,数组中靠前的位置是A中的偶数,靠后的位置是A中的奇数.偶数范围内顺序不限,奇数也是. 约定 1 <= A.length <= 5000 0 <= A[i] <= 5000 示例 Input: [3,1,2,4] Output: [2,4,3,1] The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepte

LeetCode 905. Sort Array By Parity

905. Sort Array By Parity Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A. You may return any answer array that satisfies this condition. Example 1: Input: [3,

905. Sort Array By Parity

题目来源: https://leetcode.com/problems/sort-array-by-parity/ 自我感觉难度/真实难度: easy/easy 题意: 把列表里的偶数放在前面,奇数放在后面 分析: 自己的代码: class Solution(object): def sortArrayByParity(self, A): """ :type A: List[int] :rtype: List[int] """ c=[] b=[]

Sort Array By Parity II LT922

Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even. Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even. You may return any answer array that satisfi

【LEETCODE】41、905. Sort Array By Parity

package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * @ClassName: SortArrayByParity * @Author: xiaof * @Description: 905. Sort Array By Parity * Given an array A of non-negative integers, return an array

【LEETCODE】42、922. Sort Array By Parity II

package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * @ClassName: SortArrayByParityII * @Author: xiaof * @Description: 922. Sort Array By Parity II * Given an array A of non-negative integers, half of the

Sort Array

各种 Sort 算法,包括 Quick Sort, Merge Sort, Heap Sort, Count Sort 1 package Sort; 2 3 public class Sort { 4 /* 5 * Quick Sort 6 * Time : O(n log n) 7 * Space : O(log n) -- stack frame 8 */ 9 private void quickSort(int[] arr) { 10 if (arr == null || arr.len