当前位置:网站首页>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("")
边栏推荐
猜你喜欢
Gold, silver and four want to change jobs, so we should seize the time to make up
(27) Open operation, close operation, morphological gradient, top hat, black hat
多线程使用不当导致的 OOM
How to restore the system with one click on Lenovo laptop
String类
Introduction to software engineering - Chapter 6 - detailed design
Dataframe gets the number of words in the string
Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI
sql刷题1050. 合作过至少三次的演员和导演
Rhcsa Road
随机推荐
Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
Leetcode records - sort -215, 347, 451, 75
中国冰淇淋市场深度评估及发展趋势预测报告(2022版)
Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI
String类
Free lottery | explore the future series of blind box digital copyright works of "abadou" will be launched on the whole network!
走进微信小程序
[flask introduction series] cookies and session
Redis 分布式锁
mysql -- explain性能优化
redis -- 数据类型及操作
【PyG】文档总结以及项目经验(持续更新
Rhcsa Road
【Try to Hack】vulnhub DC4
Redis distributed lock
Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
Research and investment strategy report of neutral protease industry in China (2022 Edition)
What is the effect of choosing game shield safely in the game industry?
免费抽奖 | 《阿巴豆》探索未来系列盲盒数字版权作品全网首发!
Is the securities account given by the head teacher of goucai school safe? Can I open an account?