题解 | #压缩牛群编号#

压缩牛群编号

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
};

全部评论

相关推荐

程序员小白条:三方不签,不就是纯实习骗人吗,还是小公司,没毛了
点赞 评论 收藏
分享
肖先生~:大一点得到公司面试更能学到点东西
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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