LeetCode49. Group Anagrams

文章目录

一、题目

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:

Input: strs = "eat","tea","tan","ate","nat","bat"

Output: \["bat","nat","tan","ate","eat","tea"]

Example 2:

Input: strs = ""

Output: \[""]

Example 3:

Input: strs = "a"

Output: \["a"]

Constraints:

1 <= strs.length <= 104

0 <= strsi.length <= 100

strsi consists of lowercase English letters.

二、题解

cpp 复制代码
class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        int n = strs.size();
        unordered_map<string,vector<string>> map;
        for(int i = 0;i < n;i++){
            string s = strs[i];
            sort(s.begin(),s.end());
            map[s].emplace_back(strs[i]);
        }
        vector<vector<string>> res;
        //遍历所有的key
        for(auto it = map.begin();it != map.end();it++){
            res.emplace_back(it->second);
        }
        return res;
    }
};
相关推荐
她说可以呀1 分钟前
Spring-ai-Rag-多文件格式生成 List<Document>
数据结构·list
乐观勇敢坚强的老彭6 分钟前
计算机内存中的堆和栈
c++
饼饼学习空间智能34 分钟前
遥操作数据能否提升机器人自主化水平?详解模仿学习、仿真放大与Sim2Real闭环
人工智能·算法
间歇性努力持续性发呆的野生快乐选手1 小时前
dfs回溯,bfs枚举,完全背包
c++·算法·深度优先·宽度优先
郝学胜_神的一滴1 小时前
干货版《算法导论》17:双数极值配对与卡牌手牌编码最优解
数据结构·算法
码工许师傅2 小时前
一文读懂《Effective C++ 第三版》的55条黄金法则
c++
ao-weilai2 小时前
C++基础:C++11部分内容
开发语言·c++·c++11
有点。2 小时前
C++深度优先搜索(三)
开发语言·c++·深度优先
nike0good2 小时前
Codeforces Round 1101 (Div. 2) 题解
算法
中年阿甘2 小时前
解析式布局-二次线长布局
线性代数·算法·矩阵