题解 | #字符串合并处理#

字符串合并处理

https://www.nowcoder.com/practice/d3d8e23870584782b3dd48f26cb39c8f

const rl = require("readline").createInterface({ input: process.stdin })

class Developer {
  constructor() {
    this.str = ''
  }

  get len () {
    return this.str.length
  }

  input (line) {
    if (!line) return

    const [pre, next] = line.split(' ')

    this.str = '' + pre + next

    this.sort().transfer()

    rl.close()
  }

  sort () {
    let str = ''
    let even = []
    let odd = []
    for (let i = 0; i < this.len; i ++) {
      if (i % 2 === 0) {
        even.push(this.str.charAt(i))
      } else {
        odd.push(this.str.charAt(i))
      }
    }

    even.sort()
    odd.sort()

    for (let i = 0; i < this.len; i ++) {
      const index = Math.floor(i / 2)
      if (i % 2 === 0) {
        str += even[index]
      } else {
        str += odd[index]
      }
    }

    this.str = str

    return this
  }

  transfer () {
    const arr = this.str.split('')
    for (let i = 0; i < this.len; i ++) {
      let c = arr[i]
      if (/[a-fA-F0-9]/.test(c)) {
        c = parseInt(c, 16).toString(2).padStart(4, '0')
        c = c.split('').reverse().join('')
        c = parseInt(c, 2).toString(16).toUpperCase()
        arr[i] = c
      }
    }
    this.str = arr.join('')

    return this
  }

  output () {
    console.log(this.str)
  }
}

const developer = new Developer()

rl.on('line', (line) => {
  developer.input(line)
})

rl.on('close', () => {
  developer.output()
})

全部评论

相关推荐

02-26 09:15
已编辑
蚌埠学院 golang
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务