【力扣专题栏】字母异词分组,如何利用强大的容器(unordered_map)解决该问题?

题解目录

1、题目描述+解释

2、算法原理解析

3、代码编写

cpp 复制代码
class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        //创建哈希表
        unordered_map<string,vector<string>> hash;
        //遍历
        for(auto& s:strs)
        {
            string tmp=s;
            //排序
            sort(tmp.begin(),tmp.end());
            //插入到key为tmp对应的vector中去
            hash[tmp].push_back(s);
        }
        vector<vector<string>> ret;
        for(auto& h:hash)
        {
            ret.push_back(h.second);
        }
        return ret;
    }
};
相关推荐
Pluchon2 小时前
硅基计划4.0 算法 二叉树深搜(DFS)
java·数据结构·算法·leetcode·深度优先·剪枝
sprintzer3 小时前
10.6-10.15力扣模拟刷题
算法·leetcode·职场和发展
一匹电信狗4 小时前
【C++】C++风格的类型转换
服务器·开发语言·c++·leetcode·小程序·stl·visual studio
学学学无无止境5 小时前
力扣-上升的温度
leetcode
Emilia486.7 小时前
【Leetcode&nowcode&数据结构】顺序表的应用
数据结构·算法·leetcode
Dream it possible!8 小时前
LeetCode 面试经典 150_栈_简化路径(53_71_C++_中等)(栈+stringstream)
c++·leetcode·面试·
程序员阿鹏8 小时前
49.字母异位词分组
java·开发语言·leetcode
Espresso Macchiato9 小时前
Leetcode 3715. Sum of Perfect Square Ancestors
算法·leetcode·职场和发展·leetcode hard·树的遍历·leetcode 3715·leetcode周赛471
Miraitowa_cheems15 小时前
LeetCode算法日记 - Day 73: 最小路径和、地下城游戏
数据结构·算法·leetcode·职场和发展·深度优先·动态规划·推荐算法
野蛮人6号15 小时前
力扣热题100道之560和位K的子数组
数据结构·算法·leetcode