当前位置:网站首页>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
pop
Method to take out the elements at the top of the stack - Determine whether it is an array
- yes ——
continue push(...next)
- no —— Then record
res
Array
- 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("")
边栏推荐
猜你喜欢
Leetcode records - sort -215, 347, 451, 75
Redis Distributed Lock
SQL question brushing 586 Customers with the most orders
免费抽奖 | 《阿巴豆》探索未来系列盲盒数字版权作品全网首发!
The amazing open source animation library is not only awesome, but also small
C language input / output stream and file operation
游戏行业安全选择游戏盾,效果怎么样?
Are you still using charged document management tools? I have a better choice! Completely free
How to cancel automatic search and install device drivers for laptops
ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享
随机推荐
[Supplément linguistique c] déterminer quel jour est demain (date de demain)
String class
SQL question brushing 586 Customers with the most orders
Please, stop painting star! This has nothing to do with patriotism!
Rhcsa Road
Transition technology from IPv4 to IPv6
单例模式的懒汉模式跟恶汉模式的区别
Object. fromEntries()
unity3d扩展工具栏
String的trim()和substring()详解
Chinese diosgenin market forecast and investment strategy report (2022 Edition)
Jojogan practice
redis -- 数据类型及操作
Redis Distributed Lock
【C语言补充】判断明天是哪一天(明天的日期)
Is the securities account given by the head teacher of goucai school safe? Can I open an account?
Internet News: "20220222" get together to get licenses; Many products of Jimi have been affirmed by consumers; Starbucks was fined for using expired ingredients in two stores
Soft test software designer full truth simulation question (including answer analysis)
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
【splishsplash】关于如何在GUI和json上接收/显示用户参数、MVC模式和GenParam