题解 | #字符串合并处理#
字符串合并处理
https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f
const str = readline().split(' ').join('') const evenArr = [] const oddArr = [] const sortArr = [] for(let i = 0;i<str.length;i++){ if(i%2===0){ evenArr.push(str[i]) }else{ oddArr.push(str[i]) } } evenArr.sort() oddArr.sort() let eindex = 0 let oindex = 0 let sindex = 0 let len = evenArr.length+oddArr.length while(eindex<evenArr.length||oindex<oddArr.length){ if(sindex%2===0){ sortArr.push(evenArr[eindex++]) }else{ sortArr.push(oddArr[oindex++]) } sindex++ } let resArr = [] sortArr.forEach(item=>{ let str = item if(/[0-9]/.test(item)){ str = parseInt(parseInt(item).toString(2).padStart(4,'0').split('').reverse().join(''),2).toString(16).toUpperCase() } if(/[A-Fa-f]/.test(item)){ str = parseInt(parseInt(item,16).toString(2).padStart(4,'0').split('').reverse().join(''),2).toString(16).toUpperCase() } resArr.push(str) }) console.log(resArr.join(''))