Comments (2)
解题思路:
- 如果为空数组直接返回空数组
- 遍历单词数组,为每个单词建立长度为26的内容为0的数组,同时遍历每个单词将
每个字母出现的频率记录到数组指定位置(利用ASCII码) - 将同等频率的单词放入Hash Map中
- 遍历map,将结果返回
/**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function (strs) {
let result = [];
if (strs.length === 0) return result;
let map = new Map();
for (const str of strs) {
let arr = new Array(26).fill(0);
for (let i = 0; i < str.length; i++) {
const ascii = str[i].charCodeAt() - 97;
arr[ascii]++;
}
const key = arr.join("");
if (map.has(key)) {
map.set(key, [...map.get(key), str]);
} else {
map.set(key, [str]);
}
}
for (const value of map.values()) {
result.push(value);
}
return result;
};
from like-algorithms.
哈希表解
复杂度O(NKlogK)
, N 为 strs 长度,K 为 strs 中每个字符串的长度
const len = strs.length;
if (!len) return [];
const ret = [];
const map = new Map();
for (let i = 0; i < len; i++) {
let newStr = strs[i].split('').sort().join(''); // 拆分排序合并后作为 key
if (map.has(newStr)) {
const old = map.get(newStr);
old.push(strs[i]);
map.set(newStr, old);
} else {
map.set(newStr, [strs[i]]);
}
}
return Array.from(map.values());
from like-algorithms.
Related Issues (20)
- ✅1480. 一维数组的动态和 HOT 1
- ✅406. 根据身高重建队列 HOT 1
- ✅1030. 距离顺序排列矩阵单元格 HOT 1
- ✅134. 加油站 HOT 1
- ✅3. 无重复字符的最长子串 HOT 1
- ✅6. Z 字形变换 HOT 1
- ✅452. 用最少数量的箭引爆气球 HOT 1
- ✅剑指 Offer 58 - II. 左旋转字符串 HOT 1
- ✅1370. 上升下降字符串 HOT 1
- ✅164. 最大间距 HOT 1
- ✅454. 四数相加 II HOT 1
- ✅976. 三角形的最大周长 HOT 1
- ✅767. 重构字符串 HOT 1
- ✅34. 在排序数组中查找元素的第一个和最后一个位置 HOT 2
- 📌321. 拼接最大数
- ✅659. 分割数组为连续子序列 HOT 1
- ✅78. 子集 HOT 2
- ✅860. 柠檬水找零 HOT 1
- ✅90. 子集 II HOT 1
- 46. 全排列
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from like-algorithms.