Skip to content

Commit 70a83bc

Browse files
committed
49. 字母异位词分组
1 parent 7d430c7 commit 70a83bc

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
|34|[在排序数组中查找元素的第一个和最后一个位置](https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/)|[JavaScript](./algorithms/find-first-and-last-position-of-element-in-sorted-array.js)|Medium|
2828
|35|[搜索插入位置](https://leetcode.cn/problems/search-insert-position/)|[JavaScript](./algorithms/search-insert-position.js)|Easy|
2929
|48|[旋转图像](https://leetcode.cn/problems/rotate-image/)|[JavaScript](./algorithms/rotate-image.js)|Medium|
30+
|49|[字母异位词分组](https://leetcode.cn/problems/group-anagrams/)|[JavaScript](./algorithms/group-anagrams.js)|Medium|
3031
|58|[最后一个单词的长度](https://leetcode.cn/problems/length-of-last-word/)|[JavaScript](./algorithms/length-of-last-word.js)|Easy|
3132
|53|[最大子数组和](https://leetcode.cn/problems/maximum-subarray/)|[JavaScript](./algorithms/maximum-subarray.js)|Easy|
3233
|66|[加一](https://leetcode-cn.com/problems/plus-one/)|[JavaScript](./algorithms/plus-one.js)|Easy|

algorithms/group-anagrams.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string[]} strs
3+
* @return {string[][]}
4+
*/
5+
var groupAnagrams = function (strs) {
6+
const map = new Map();
7+
8+
for (let word of strs) {
9+
let temp = word.split("").sort().join("");
10+
11+
if (map.has(temp)) {
12+
map.get(temp).push(word);
13+
} else {
14+
map.set(temp, [word]);
15+
}
16+
}
17+
18+
return Array.from(map.values());
19+
};

0 commit comments

Comments
 (0)