当前位置:网站首页>Integer array merge [JS]
Integer array merge [JS]
2022-07-01 17:02:00 【qq_ twenty-two million eight hundred and forty-one thousand thr】
Problem description
Topic link - Cattle guest
Merge two arrays of integers in ascending order , And filter out duplicate array elements .
When outputting, there is no space between two adjacent numbers .
Input description :
Enter description , Enter... In the following order :
- Enter the number of the first array
- Enter the value of the first array
- Enter the number of the second array
- Enter the value of the second array
Output description :
Output the merged array
Example 1
Input :
3
1 2 5
4
-1 0 3 2
Output :
-101235
Solved JS Code
let arr_num = 2;
let num;
let ans = [];
while (arr_num--) {
num = readline();
let temp = readline();
temp = temp.split(" ");
temp = Array.from(new Set(temp));
temp = temp.sort();
ans.push(temp);
}
function flat(input){
const stack = [...input];
const res = [];
while(stack.length){
const next = stack.pop();
if(Array.isArray(next)){
stack.push(...next);
}else{
res.push(next);
}
}
return res.reverse();
}
ans = flat(ans);
ans = Array.from(new Set(ans));
ans.sort((a , b) => {
return a - b;
})
ans = ans.join("")
console.log(ans);
Code interpretation
The first time I saw this topic, I thought JS Array de duplication in Set, So choose to use js To write the title
First of all, the idea of this topic is actually very simple , After input , Ascending , duplicate removal , Then the output
Actually , At the beginning , I didn't even notice , At first glance, I was attracted by the array , I didn't notice Ascending also You need to enter four numbers , Then there are a few js Basic questions , Submitted a total of 6 Time ……
A few points to be noted
1.readline yes string
I passed readline Function reads the input characters , At first I took it for granted that this was an array , Errors are always reported when processing the results , Prototype chains cannot be used Array The method in
So using split(" ") Split string , Into an array
2. Can't use flat
After processing , Become a two-dimensional array , So it's like passing flat The method is flattened directly , But Niuke js The environment does not seem to support this method , So I wrote a flat Method
Actually flat The source code of is also quite simple
- adopt
...The extension operator deconstructs the input value - while loop stack The length of
- adopt
popMethod to take out the elements at the top of the stack - Determine whether it is an array
- yes ——
continue push(...next) - no —— Then record
resArray
- yes ——
- adopt
After completion , Remember to pass reverse Function returns an array in the original order , Because it is traversed from back to front , So we need to reverse the order
function flat(input){
const stack = [...input];
const res = [];
while(stack.length){
const next = stack.pop();
if(Array.isArray(next)){
stack.push(...next);
}else{
res.push(next);
}
}
return res.reverse();
}
3. Array weight removal
Directly through Array.from(new Set( Array )) You can complete , To borrow Set The data structure of is completed
4.sort The sorting method of functions
sort The of the function is through ASCII Code to sort , So this happens
The original array is 2,3,6,8,11
It turns out to be 11 2 3 6 8
because 1 Of ASCII Smaller size , So it was put in the first place , So we need to rewrite
ans.sort((a , b) => {
return a - b;
})
If descending order is required , take a,b The other way around
5. Array to string ——join
The final question result needs to be output Without spaces The output format of , So you need to convert the array into a string
Directly through join Function conversion is enough
ans = ans.join("")
边栏推荐
- 多线程使用不当导致的 OOM
- Redis 分布式锁
- China carbon disulfide industry research and investment strategy report (2022 Edition)
- Rhcsa Road
- 中国茂金属聚乙烯(mPE)行业研究报告(2022版)
- 多线程并发之CountDownLatch阻塞等待
- 英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放
- What are the differences between PHP and DW
- 判断二叉树是否为二叉搜索树
- Mysql database - Advanced SQL statement (2)
猜你喜欢

How to maintain the laptop battery

美国国家安全局(NSA)“酸狐狸”漏洞攻击武器平台技术分析报告

How wild are hackers' ways of making money? CTF reverse entry Guide

String类

【牛客网刷题系列 之 Verilog快速入门】~ 优先编码器电路①

【flask入门系列】Cookie与Session

National Security Agency (NSA) "sour Fox" vulnerability attack weapon platform technical analysis report

多线程并发之CountDownLatch阻塞等待

There is a new breakthrough in quantum field: the duration of quantum state can exceed 5 seconds

Machine learning 11 clustering, outlier discrimination
随机推荐
Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
Redis Distributed Lock
Computed property “xxx“ was assigned to but it has no setter.
Hi Fun Summer, play SQL planner with starrocks!
多线程并发之CountDownLatch阻塞等待
PR basic clip operation / video export operation
SQL question brushing 586 Customers with the most orders
In aks, use secret in CSI driver mount key vault
《中国智慧环保产业发展监测与投资前景研究报告(2022版)》
(27) Open operation, close operation, morphological gradient, top hat, black hat
Flux d'entrées / sorties et opérations de fichiers en langage C
求求你们,别再刷 Star 了!这跟“爱国”没关系!
[C language supplement] judge which day tomorrow is (tomorrow's date)
阿里云、追一科技抢滩对话式AI
智能运维实战:银行业务流程及单笔交易追踪
How to use etcd to realize distributed /etc directory
Redis 分布式锁
National Security Agency (NSA) "sour Fox" vulnerability attack weapon platform technical analysis report
How wild are hackers' ways of making money? CTF reverse entry Guide
[Supplément linguistique c] déterminer quel jour est demain (date de demain)