【LeetCode热题100(2/100)】字母异位词分组
题目地址:链接
题目描述: 给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。字母异位词 是由重新排列源单词的所有字母得到的一个新单词。
示例输出:
输入: strs = [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”]
输出: [[“bat”],[“nat”,“tan”],[“ate”,“eat”,“tea”]]
哈希表
思路: 将字符串转换为数组,排序后,再转换为字符串key,通过key值进行判断是否可以组合在一起
var groupAnagrams = function(strs) {
let map = {}
for(let s of strs) {
let key = s.split("");
key.sort()
key = st.join("");
map[key] != undefined ? map[key].push(s): map[key] = [s];
}
return Object.values(map);
};
8万+

被折叠的 条评论
为什么被折叠?



