首页 > 其他分享 >leetcode 49 字母异位词分组

leetcode 49 字母异位词分组

时间:2024-02-14 23:24:56浏览次数:27  
标签:map hash 49 strs 异位 vector result key leetcode

 

 

需要好好研究各种写法。

C++解法

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        vector<vector<string>> result;
        if (strs.size() == 0) return result;
        unordered_map<string, vector<string>> hash_map;

        for (int i = 0; i < strs.size(); i++)
        {
            string key = strs[i];
            sort(key.begin(),key.end());

            /*
            if (hash_map.count(key) == 0)
            {
                hash_map.insert({ key, vector<string>() });
            }
            */
            
            hash_map[key].push_back(strs[i]);
        }

        for (auto it = hash_map.begin(); it != hash_map.end(); it++)
        {
            vector<string>& value = it->second;
            result.push_back(value);
        }

        return result;
    }
};

 

标签:map,hash,49,strs,异位,vector,result,key,leetcode
From: https://www.cnblogs.com/repinkply/p/18015834

相关文章

  • leetcode 17 电话号码的字母组合
     解题关键点:用递归方法classSolution{public:vector<string>mapping={"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};voidcom......
  • leetcode 438 找到字符串中所有字母异位词
     这个题目的有些类似实现strStr这个算法题目。解题关键点:1.采用类似滑动窗口的算法遍历字符串s。2.用两个哈希表保存字符串s和字符串p,中每个小写字母出现的次数。C++代码:classSolution{public:boolequals(vector<int>&sc,vector<int>&pc){for......
  • leetcode——数组算法——前缀和构建和应用
    leetcode——数组算法——前缀和构建和应用前缀和技巧适用于快速、频繁地计算一个索引区间内的元素之和303.区域和检索-数组不可变比如leetcode303.区域和(检索-数组不可变)题目介绍:给定一个整数数组nums,处理以下类型的多个查询:计算索引left和right(包含left......
  • [LeetCode] 2108. Find First Palindromic String in the Array
    Givenanarrayofstringswords,returnthefirstpalindromicstringinthearray.Ifthereisnosuchstring,returnanemptystring"".Astringispalindromicifitreadsthesameforwardandbackward.Example1:Input:words=["abc&quo......
  • Leetcode刷题第十天-回溯
    ......
  • P4933 大师
    原题链接题解对于任意剩余塔,都可以表示为以某个塔结尾的等差数列code#include<bits/stdc++.h>usingnamespacestd;inth[1005]={0};intdp[1005][40005]={0};//代表以塔i结尾,等差为j的种类inthaxi(intx){returnx+20001;}intmain(){intn;cin>>n;......
  • Leetcode 1691. 堆叠长方体的最大高度
    https://leetcode.cn/problems/maximum-height-by-stacking-cuboids/description/给你n个长方体cuboids,其中第i个长方体的长宽高表示为cuboids[i]=[widthi,lengthi,heighti](下标从0开始)。请你从cuboids选出一个子集,并将它们堆叠起来。如果widthi<=widthj......
  • [LeetCode] 2641. Cousins in Binary Tree II
    Giventherootofabinarytree,replacethevalueofeachnodeinthetreewiththesumofallitscousins'values.Twonodesofabinarytreearecousinsiftheyhavethesamedepthwithdifferentparents.Returntherootofthemodifiedtree.Note......
  • Leetcode刷题第九天-回溯
    113:路径总和II链接:113.路径总和II-力扣(LeetCode)root=[-2,null,-3],targetSum=-5莫要忘记负数情况......
  • Apache Ofbiz CVE-2020-9496
    ofbiz官网:https://ofbiz.apache.org/比较好的参考文章:https://blog.csdn.net/weixin_45728976/article/details/108872281影响版本ApacheOfbiz:<17.12.04漏洞原理:访问未授权的XML-RPC接口,构造XML-RPC协议请求格式,利用XML-RPC协议进行反序列化XML-RPC介绍:XML-RPC(XMLRemot......