题解 | #字符串合并处理#
字符串合并处理
http://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
又是暴力的一天
const str = readline() const stra = str.split(' ')[0]; const strb = str.split(' ')[1]; let temps = `${stra}${strb}`.split(""); const n = temps.length // 区分奇偶位置 var sa = []; var sb = []; let i = 0; while(i < n){ if(i % 2) { sb.push(temps.shift()) } else { sa.push(temps.shift()) } i++; } i = 0; // 排序 sb.sort(); sa.sort(); // 排序后的数组合并放回原数组 while(!!sa.length && !!sb.length) { temps.push(sa.shift()) temps.push(sb.shift()) // console.log(temps, !!sa.length && !!sb.length) } let tempAA = sa; if(!tempAA.length) { tempAA = sb } while(tempAA.length) { temps.push(tempAA.shift()) } // 需要转换的字符和数字 var ssstatus = { "0": "0", "1": "8" , "2": "4", "3": "C", "4": "2", "5": "A", "6": "6", "7": "E", "8": "1", "9": "9", "A": "5", "B": "D", "C": "3", "D": "B", "E": "7", "F": "F", } const res = temps.map(item => { return ssstatus[item.toUpperCase()] || item; }); console.log(res.join(''));