题解 | #数据分类处理#
数据分类处理
https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd
const rl = require("readline").createInterface({ input: process.stdin })
// https://www.nowcoder.com/practice/9a763ed59c7243bd8ab706b2da52b7fd?tpId=37&rp=1&ru=%2Fexam%2Foj%2Fta&qru=%2Fexam%2Foj%2Fta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D37%26type%3D37&difficulty=&judgeStatus=&tags=&title=&gioEnter=menu
class Developer {
constructor() {
this.line_count = 0
this.l_arr = []
this.l_num = 0
this.r_arr = []
this.r_num = 0
this.data = []
}
static _bubbleSort (arr) {
for (let i = 0; i < arr.length; i ++) {
for (let j = 0; j < arr.length - i; j ++) {
if (parseInt(arr[j]) > parseInt(arr[j + 1])) {
arr[j] = [arr[j + 1], arr[j + 1] = arr[j]][0]
}
}
}
return arr
}
input (line) {
const [num, ...rest] = line.split(' ')
if (this.line_count === 0) {
this.l_arr = rest
this.l_num = num
}
if (this.line_count === 1) {
this.r_arr = [...new Set(Developer._bubbleSort(rest))]
this.r_num = num
this.setData()
rl.close()
return
}
this.line_count ++
}
setData () {
let data = []
for (let i = 0; i < this.r_arr.length; i ++) {
const r_item = this.r_arr[i]
data = data.concat(this.getMatch(r_item))
}
if (data.length) {
data.unshift(data.length)
}
this.data = data
}
getMatch (r_item) {
const fn = (str, subStr) => {
return str.indexOf(subStr) !== -1
}
const arr = []
let num = 0 // 存在几个
for (let i = 0; i < this.l_arr.length; i ++) {
if (fn(this.l_arr[i], r_item)) {
num ++
arr.push(i)
arr.push(this.l_arr[i])
}
}
if (num > 0) {
arr.unshift(num)
arr.unshift(r_item)
}
return arr
}
output () {
console.log(this.data.join(' '))
}
}
const developer = new Developer()
rl.on('line', (line) => {
developer.input(line)
})
rl.on('close', (line) => {
developer.output()
})
OPPO公司福利 1276人发布