zoukankan      html  css  js  c++  java
  • 刷题-力扣-49. 字母异位词分组

    49. 字母异位词分组

    题目链接

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/group-anagrams/
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    题目描述

    给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。
    示例:

    输入: ["eat", "tea", "tan", "ate", "nat", "bat"]
    输出:
    [
      ["ate","eat","tea"],
      ["nat","tan"],
      ["bat"]
    ]
    

    说明:

    • 所有输入均为小写字母。
    • 不考虑答案输出的顺序。

    题目分析

    1. 根据题目描述,将字母异位词分组
    2. 对分词进行排序后归类

    代码

    class Solution {
    public:
        vector<vector<string>> groupAnagrams(vector<string>& strs) {
            unordered_map<string, vector<string>> map;
            vector<vector<string>> res;
            for (auto& s : strs) {
                string k = s;
                sort(k.begin(), k.end());
                map[k].emplace_back(s);
            }
            for (auto iter = map.begin(); iter != map.end(); ++iter) res.emplace_back(iter->second);
            return res;
        }
    };
    
  • 相关阅读:
    php上传excle文件,csv文件解析为二维数组
    transition的使用
    数组
    快捷键
    SCSS历史介绍与配置
    18-async函数
    this的指向问题
    媒体查询
    13-Set和Map数据结构
    15-Iterator和for…of循环
  • 原文地址:https://www.cnblogs.com/HanYG/p/14676565.html
Copyright © 2011-2022 走看看