【HackerRank】Missing Numbers
Numeros, The Artist, had two lists A and B, such that, B was a permutation of A. Numeros was very proud of these lists. Unfortunately, while transporting them from one exhibition to another, some numbers from List A got left out. Can you find out the numbers missing from A?
Notes
- If a number occurs multiple times in the lists, you must ensure that the frequency of that number in both the lists is the same. If that is not the case, then it is also a missing number.
- You have to print all the missing numbers in ascending order.
- Print each missing number once, even if it is missing multiple times.
- The difference between maximum and minimum number in the list B is less than or equal to 100.
Input Format
There will be four lines of input:
n - the size of the first list
This is followed by n space separated integers that make up the first list.
m - the size of the second list
This is followed by m space separated integers that make up the second list.
Output Format
Output the missing numbers in ascending order
Constraints
1<= n,m <= 1000010
1 <= x <= 10000 , x ∈ B
Xmax - Xmin < 101
题解:设置两个数组,因为x的范围在1~10000之间,只要开两个10001的数组分别记录A和B中元素的个数,然后比较两个数组就可以了。
代码如下:
1 import java.util.*; 2 3 public class Solution { 4 public static void main(String[] args) { 5 Scanner in = new Scanner(System.in); 6 int[] CountA = new int[10005]; 7 int[] CountB = new int[10005]; 8 9 int n = in.nextInt(); 10 int[] a = new int[n]; 11 for(int i = 0;i < n;i++){ 12 a[i]=in.nextInt(); 13 CountA[a[i]]++; 14 } 15 16 int m = in.nextInt(); 17 int[] b = new int[m]; 18 for(int i = 0;i < m;i++){ 19 b[i]=in.nextInt(); 20 CountB[b[i]]++; 21 } 22 23 for(int i = 1;i <= 10000;i++){ 24 if(CountB[i]>CountA[i] ) 25 System.out.printf("%d ", i); 26 } 27 System.out.println(); 28 29 30 31 } 32 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了