题解 | #字符串合并处理#
字符串合并处理
https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f?tpId=37&tags=&title=&difficulty=0&judgeStatus=0&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37
数组排序是0,1,2,3,所以偶数(把0算在内的话)要么与奇数数量相等,要么多一个,所以在下面先插入偶数,在赋值时也是先偶数数组进行赋值
while(line = await readline()){
let line1 =line.split(' ')
res = line1[0]+line1[1]
line2 = res.split('')
arr1=[]
arr2=[]
x =true
for (let i= 0;i<line2.length;i++){
if(x) arr1.push(line2[i]),x=false//偶数下标数组,交叉插入
else arr2.push(line2[i]),x =true//奇数下标数组,交叉插入
}
arr1.sort()//排序
arr2.sort()
let a=0,b=0,y=true
for(let i= 0;i<line2.length;i++){
if(y) line2[i] =arr1[a],a++,y=false//交叉给原数组赋值
else line2[i]= arr2[b],b++,y=true
}
let line3 = line2.join('')
//下面是进行符号转换
line3 = line3.replace(/[0-9a-fA-F]/g,e=>{
let a = parseInt(e,16).toString(2)//16进制转2进制
b = a.split('')
while(b.length<4) b.unshift('0')//如果2进制位数不足4则向开头添加0,直到有4位数为止
c = b.reverse().join('')
d = parseInt(c,2).toString(16)//2进制转16进制
if(d.charCodeAt(0)>=97&&d.charCodeAt(0)<=122) return d.toUpperCase()//将转换进制后为小写的字母转换为大写字母
return d//返回最终结果
})
console.log(line3)
}