题解 | #牛棚品种分类#

牛棚品种分类

https://www.nowcoder.com/practice/0b6068f804b9426aa737ea8606e8d5c3

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param strs string字符串一维数组 
 * @return string字符串一维数组
 */
function groupAnagrams(strs) {
    // write code here
    let res = []

    let copyStr = [...strs]

    while (copyStr.length > 0) {
        let string = ""
        let currStr = copyStr[0]
        
        string += currStr
        let sortStr = [...currStr].sort().join('')

        copyStr.forEach((str, index) => {
            if ([...str].sort().join('') === sortStr && str !== currStr) {
                string += ',' + str
                copyStr.splice(index, 1)
            }
        })

        copyStr.splice(0, 1)

        res.push(string)
    }

    return res.sort()
}
module.exports = {
    groupAnagrams: groupAnagrams
};

全部评论

相关推荐

Twilight_m...:经典我朋友XXXX起手,这是那种经典的不知道目前行情搁那儿胡编乱造瞎指导的中年人,不用理这种**
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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