We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
原题链接:https://leetcode-cn.com/problems/group-anagrams/
解题思路:
如果你对这题不熟悉,可以先做242. 有效的字母异位词,以及我的题解LeetCode题解:242. 有效的字母异位词,数组计数,JavaScript,详细注释。
/** * @param {string[]} strs * @return {string[][]} */ var groupAnagrams = function (strs) { let resultMap = new Map(); // 使用Map保存结果 for (const str of strs) { let keyArr = new Array(26).fill(0); // 使用数组统计所有字母的个数 for (const char of str) { // 计算字母与a的码点差值,其差值会在0-25 const index = char.codePointAt(0) - 'a'.codePointAt(0); // 每个字母的相应数量+1 keyArr[index]++; } // 将数组转换成字符串,用于保存相应异位词的结果 const key = JSON.stringify(keyArr); // 将异位词按照对应的key存储到Map中 resultMap.get(key) ? resultMap.get(key).push(str) : resultMap.set(key, [str]); } // 将Map转换为数组并输出结果 return [...resultMap.values()]; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接:https://leetcode-cn.com/problems/group-anagrams/
解题思路:
如果你对这题不熟悉,可以先做242. 有效的字母异位词,以及我的题解LeetCode题解:242. 有效的字母异位词,数组计数,JavaScript,详细注释。
The text was updated successfully, but these errors were encountered: