题解 | #压缩牛群编号#
压缩牛群编号
https://www.nowcoder.com/practice/db9dd240e5f54b6d8eeadfbd9b7f865f
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param chars char字符型一维数组
* @return char字符型一维数组
*/
function compress(chars) {
let res = []
let stack = []
for (let i = 0; i < chars.length; i++) {
stack.push(chars[i])
if (chars[i] !== chars[i + 1]) {
res.push(stack[0])
if (stack.length > 1) {
let str = stack.length.toString()
res.push(...str.split(''))
}
stack = []
}
}
return res
}
module.exports = {
compress: compress
};
查看22道真题和解析