当前位置:网站首页>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("")
边栏推荐
猜你喜欢

英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放

Kali install Nessus

Free lottery | explore the future series of blind box digital copyright works of "abadou" will be launched on the whole network!

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

6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单

如何写出好代码 — 防御式编程指南

在MeterSphere接口测试中如何使用JMeter函数和MockJS函数

How to use F1 to F12 correctly on laptop keyboard

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

Template engine velocity Foundation
随机推荐
Research and investment strategy report of hydroxypropyl beta cyclodextrin industry in China (2022 Edition)
中国茂金属聚乙烯(mPE)行业研究报告(2022版)
多线程并发之CountDownLatch阻塞等待
【C語言補充】判斷明天是哪一天(明天的日期)
libcurl下载文件的代码示例
Free lottery | explore the future series of blind box digital copyright works of "abadou" will be launched on the whole network!
Redis 分布式鎖
Machine learning 11 clustering, outlier discrimination
Babbitt | yuan universe daily must read: Naixue coin, Yuan universe paradise, virtual stock game Do you understand Naixue's tea's marketing campaign of "operation pull full"
China sorbitol Market Forecast and investment strategy report (2022 Edition)
Girls who want to do software testing look here
China carbon disulfide industry research and investment strategy report (2022 Edition)
Shenyu gateway development: enable and run locally
What is the effect of choosing game shield safely in the game industry?
越来越多地使用 SLO 来实现可观测性|DevOps
重磅披露!上百个重要信息系统被入侵,主机成为重点攻击目标
SQL question brushing 586 Customers with the most orders
Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
sql刷题586. 订单最多的客户
Object. fromEntries()