鱼C论坛

 找回密码
 立即注册
查看: 1904|回复: 0

[学习笔记] leetcode 1122. Relative Sort Array

[复制链接]
发表于 2019-12-24 14:15:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

  2. 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.



  3. Example 1:

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


  6. Constraints:

  7. arr1.length, arr2.length <= 1000
  8. 0 <= arr1[i], arr2[i] <= 1000
  9. Each arr2[i] is distinct.
  10. Each arr2[i] is in arr1.
复制代码

  1. class Solution {
  2.     public int[] relativeSortArray(int[] arr1, int[] arr2) {
  3.         if(arr1.length == 0 || arr2.length == 0) return arr1;
  4.         Map <Integer , Integer> map = new HashMap<Integer, Integer>();
  5.         for(int i = 0; i< arr1.length ; i++){
  6.             if(!map.containsKey(arr1[i])){
  7.                 map.put(arr1[i],1);
  8.             }else{
  9.                 map.put(arr1[i],map.get(arr1[i])+1);
  10.             }
  11.         }
  12.         
  13.         int[] ret = new int[arr1.length];
  14.         int i = 0;
  15.         for(int k : arr2){
  16.             while(map.get(k) != 0){
  17.                 ret[i] = k;
  18.                 map.put(k,map.get(k)-1);
  19.                 i++;
  20.             }
  21.             
  22.             if(map.get(k) == 0){
  23.                 map.remove(k);
  24.             }
  25.         }
  26.         
  27.         PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
  28.         for(Integer k : map.keySet()){
  29.             while(map.get(k) != 0){
  30.                 queue.add(k);
  31.                 map.put(k,map.get(k)-1);
  32.             }
  33.         }
  34.         
  35.         while(!queue.isEmpty()){
  36.             ret[i] = queue.poll();
  37.             i++;
  38.         }
  39.         
  40.         return ret;
  41.     }
  42. }
复制代码

  1. class Solution {
  2.     public int[] relativeSortArray(int[] arr1, int[] arr2) {
  3.         if(arr1.length == 0 || arr2.length == 0) return arr1;
  4.         
  5.         int[] ret = new int[1001];
  6.         
  7.         for(int i : arr1){
  8.             ret[i]++;
  9.         }
  10.         
  11.         int j = 0;
  12.         for(int i : arr2){
  13.             while(ret[i]-- > 0){
  14.                 arr1[j++] = i;
  15.             }
  16.         }
  17.         
  18.         for(int i = 0; i< 1001; i++){
  19.             while(ret[i]-- > 0){
  20.                 arr1[j++] = i;
  21.             }
  22.         }
  23.         
  24.         return arr1;
  25.     }
  26. }
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 09:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表