当前位置:网站首页>整形数组合并【JS】
整形数组合并【JS】
2022-07-01 16:46:00 【qq_22841387】
问题描述
题目链接-牛客
将两个整型数组按照升序合并,并且过滤掉重复数组元素。
输出时相邻两数之间没有空格。
输入描述:
输入说明,按下列顺序输入:
- 输入第一个数组的个数
- 输入第一个数组的数值
- 输入第二个数组的个数
- 输入第二个数组的数值
输出描述:
输出合并之后的数组
示例1
输入:
3
1 2 5
4
-1 0 3 2
输出:
-101235
解决的JS代码
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);
代码解释
看见这个题目的第一眼就想到了JS中的数组去重Set,所以就选择使用js去写题目了
首先这个题目其实思路很简单,就是输入后,升序,去重,然后输出
其实,开始的时候,我都没注意到,就第一眼被数组去重吸引了,就没注意到升序还有需要输入四个数,然后还有几个js基础问题,提交了一共6次……
需要注意的几个点
1.readline是string
我通过readline函数读取输入的字符,开始我想当然的以为这就是一个数组,结果处理的时候一直报错,不能使用原型链Array中的方法
于是使用split(" ")分割字符串,变为数组
2.无法使用flat
处理后,变成一个二维数组,于是就像通过flat方法直接扁平化,但是牛客的js环境貌似不支持该方法,于是就手写了一个flat方法
其实flat的源码也挺简单的
- 通过
...扩展运算符解构输入的值 - while循环stack的长度
- 通过
pop方法拿出栈头的元素 - 判断是否为数组
- 是——
继续push(...next) - 否——则记录到
res数组中
- 是——
- 通过
完成后,记得通过reverse函数返回原顺序的数组,因为是从后至前去遍历的,所以需要逆序一下
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.from(new Set(数组))即可完成数组区中,借用Set的数据结构完成
4.sort函数的排序方式
sort函数的是通过ASCII码去排序的,所以会出现这样的情况
原数组为2,3,6,8,11
结果却为11 2 3 6 8
因为1的ASCII码更小,所以被放在了第一位,所以需要重写
ans.sort((a , b) => {
return a - b;
})
若需要降序,将a,b两者反过来即可
5.数组转字符串——join
最后题目结果需要输出的是不带空格的输出格式,所以需要将数组转换为字符串
直接通过join函数转换即可
ans = ans.join("")
边栏推荐
- Please, stop painting star! This has nothing to do with patriotism!
- 中国一次性卫生用品生产设备行业深度调研报告(2022版)
- 挖财学堂班主任给的证券账户安全吗?能开户吗?
- Installation and use of sqoop
- National Security Agency (NSA) "sour Fox" vulnerability attack weapon platform technical analysis report
- Advantages, values and risks of chain games compared with traditional games
- Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!
- Redis6.0 new features
- 【flask入门系列】Cookie与Session
- 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
猜你喜欢

SQL注入漏洞(Mysql与MSSQL特性)

ACL 2022 | 分解的元学习小样本命名实体识别

字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化

模板引擎Velocity 基础

PR basic clip operation / video export operation

Redis6.0 new features

String类

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

走进微信小程序

Gold, silver and four want to change jobs, so we should seize the time to make up
随机推荐
今天14:00 | 港大、北航、耶鲁、清华、加大等15位ICLR一作讲者精彩继续!
Tutorial on the principle and application of database system (001) -- MySQL installation and configuration: installation of MySQL software (Windows Environment)
China carbon disulfide industry research and investment strategy report (2022 Edition)
Buuctf gold III
剑指 Offer II 015. 字符串中的所有变位词
巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...
[pyg] document summary and project experience (continuously updated
为什么你要考虑使用Prisma
How to maintain the laptop battery
[wrung Ba wrung Ba is 20] [essay] why should I learn this in college?
SQL question brushing 1050 Actors and directors who have worked together at least three times
Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!
China benzene hydrogenation Market Research and investment forecast report (2022 Edition)
Judge whether a binary tree is a balanced binary tree
China sorbitol Market Forecast and investment strategy report (2022 Edition)
Rhcsa Road
中国氮化硅陶瓷基板行业研究与投资前景报告(2022版)
How to solve the keyboard key failure of notebook computer
字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
Hidden Markov model (HMM): model parameter estimation