LeeCode-刷题日记-Day21
49. 字母异位词分组
思路:异位词中的每个字符出现次数相同。将字符出现次数存入数组中。用次数数组作为key
和字符串作为value
建立对应关系,如果次数相同则加入对应的value数组。不同则创建新的key-value
关系
/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function(strs) {
const map = new Object();
for(let s of strs){
const count= new Array(26).fill(0);
for(let c of s){
count[c.charCodeAt()-'a'.charCodeAt()]++;
}
map[count]?map[count].push(s):map[count]=[s];
}
return Object.values(map);
};
- 本文链接:https://archer-lan.github.io/2023/11/20/LeeCode-%E5%88%B7%E9%A2%98%E6%97%A5%E8%AE%B0-Day21/
- 版权声明:本博客所有文章除特别声明外,均默认采用 许可协议。