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;
    }
};
相关推荐
a187927218314 小时前
【算法】双指针与滑动窗口(三):相向双指针——比较、排除、收缩
算法·leetcode·双指针·滑动窗口·原理·相向双指针·算法讲解
代码王同学4 小时前
2026.9.15日C++类和对象学习收获笔记
c语言·c++
刚入门的大一新生4 小时前
Linux-简单设计libc库
linux·c++
AI 小老六4 小时前
Agent 记忆系统难在取舍
人工智能·算法·架构·agent·memory·harness
大熊背4 小时前
《Color constancy by characterization of illumination chromaticity》之色度色域最大化算法(一)
数码相机·算法·白平衡·色度色域
不会就选b4 小时前
算法日常・每日刷题--<贪心>13
算法
带多刺的玫瑰4 小时前
Leecode#35刷题之搜索插入位置
java·python·算法
我不会起名字3225 小时前
万字总结Golang入门项目
数据结构·经验分享·算法·golang·项目实战·复盘
6Hzlia5 小时前
【Classic 150 刷题计划】 LeetCode 14. 最长公共前缀 | C++ 纵向扫描法与防越界细节
c++·算法·leetcode
Niuguangshuo13 小时前
论文解读:CTC,端到端序列识别的开山之作
算法·语音识别