49. Group Anagrams
Given an array of strings, group anagrams together.
Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"]
,
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]题意:对数组项进行分类,具有相同字母的项为一组代码如下:
/** * @param {string[]} strs * @return {string[][]} */ var groupAnagrams = function(strs) { var len=strs.length; var str=[]; var res=new Map(); for(var i=0;i<len;i++){ //取键 str=strs[i].split(‘‘).sort().join(‘‘); //存值 if(!res.has(str)) res.set(str,new Array()) res.get(str).push(strs[i]); } return [...res.values()] };
原文地址:https://www.cnblogs.com/xingguozhiming/p/10428466.html
时间: 2024-10-09 12:38:51