-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path49_javascript.js
55 lines (47 loc) · 1.22 KB
/
49_javascript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。
// 示例:
// 输入: ["eat", "tea", "tan", "ate", "nat", "bat"]
// 输出:
// [
// ["ate","eat","tea"],
// ["nat","tan"],
// ["bat"]
// ]
// 说明:
// 所有输入均为小写字母。
// 不考虑答案输出的顺序。
//
/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function (strs) {
let map = new Map();
let newStr = [],
res = [];
// 将数组字符串序列化为同样的值
// newStr = ['aet','aet','ant','aet','ant','abt' ]
for (let i = 0; i < strs.length; i++) {
newStr[i] = strs[i].split("").sort().join("");
}
// 获取map表 获取的map表类似这样
// {
// 'aet' : ["ate","eat","tea"],
// 'ant' : ["nat","tan"],
// 'abt' : ["bat"],
// }
for (let j = 0; j < newStr.length; j++) {
if (!map.get(newStr[j])) {
map.set(newStr[j], [strs[j]]);
} else {
map.set(newStr[j], [...map.get(newStr[j]), strs[j]]);
}
}
// 将map表的value返回
map.forEach((value, key) => {
res.push(value);
});
return res;
// 返回map表也可以简化为
// return Array.from(map.values)
};