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 %

完成日期:03/12/2020

关键点:建立 array cnt 计数

class Solution {
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        int[] cnt = new int[1001];
        // save counts from arr1 into cnt
        for(int n : arr1)
            cnt[n]++;

        // put same arr2 numbers into arr1 from cnt according to arr2 order
        int i = 0;
        for(int n : arr2) {
            while(cnt[n]-- > 0){
                arr1[i++] = n;
            }
        }

        // put the rest number from arr1 into cnt; cnt is in order already.
        for(int n = 0; n < cnt.length; n++) {
            while(cnt[n]-- > 0) {
                arr1[i++] = n;
            }
        }

        return arr1;
    }
}

参考资料:LeetCode Discuss

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

原文地址:https://www.cnblogs.com/jimmycheng/p/12495783.html

时间: 2024-10-18 17:12:36

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

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

【leetcode】905. Sort Array By Parity

题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数组头部,否则追加到尾部. 代码如下: class Solution(object): def sortArrayByParity(self, A): """ :type A: List[int] :rtype: List[int] """ res =

[LeetCode] Longest Mountain in Array 数组中最长的山

Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold: B.length >= 3 There exists some 0 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1] (Note tha

【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,

Array数组集合的排序

/* ######### ############ ############# ## ########### ### ###### ##### ### ####### #### ### ########## #### #### ########### #### #### ########### ##### ##### ### ######## ##### ##### ### ######## ###### ###### ### ########### ###### ###### #### ###

leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)

题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For example,Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 说明: 1)设个标志可实现 实现: 1 class Solution { 2 public

Array.prototype.sort()对数组对象排序的方法

Array.prototype.sort()方法接受一个参数——Function,Function会提供两个参数,分别是两个进行比较的元素,如果元素是String类型则通过Unicode code进行比较,如果是Number类型则比较值的大小.如果比较的函数中返回1则两个元素交换位置,0和-1不交换位置. var arr = [3, 5, 2, 1]; // 从小到大排序 arr.sort(function(a, b){ return a > b ? 1 : -1; }); // 得到的结果:[

[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,