首页 > 其他分享 >LeetCode 49. 字母异位词分组(/hash)

LeetCode 49. 字母异位词分组(/hash)

时间:2023-02-22 13:23:28浏览次数:51  
标签:hash 49 back vector mp str ans return LeetCode

原题解

题目

约束

题解

解法一


class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string, vector<string>> mp;
        for (string& str: strs) {
            string key = str;
            sort(key.begin(), key.end());
            mp[key].emplace_back(str);
        }
        vector<vector<string>> ans;
        for (auto it = mp.begin(); it != mp.end(); ++it) {
            ans.emplace_back(it->second);
        }
        return ans;
    }
};

解法二


class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        // 自定义对 array<int, 26> 类型的哈希函数
        auto arrayHash = [fn = hash<int>{}] (const array<int, 26>& arr) -> size_t {
            return accumulate(arr.begin(), arr.end(), 0u, [&](size_t acc, int num) {
                return (acc << 1) ^ fn(num);
            });
        };

        unordered_map<array<int, 26>, vector<string>, decltype(arrayHash)> mp(0, arrayHash);
        for (string& str: strs) {
            array<int, 26> counts{};
            int length = str.length();
            for (int i = 0; i < length; ++i) {
                counts[str[i] - 'a'] ++;
            }
            mp[counts].emplace_back(str);
        }
        vector<vector<string>> ans;
        for (auto it = mp.begin(); it != mp.end(); ++it) {
            ans.emplace_back(it->second);
        }
        return ans;
    }
};

标签:hash,49,back,vector,mp,str,ans,return,LeetCode
From: https://www.cnblogs.com/chuixulvcao/p/17143994.html

相关文章

  • 算法19:LeetCode_二叉树序列化与反序列化(层序)
    ​ 本题为链接为https://leetcode.cn/problems/serialize-and-deserialize-binary-tree想要搞懂本题,请先阅读我之前写的关于二叉树层序遍历文章算法8:LeetCode_二叉树的......
  • 【LeetCode二叉树#03】翻转二叉树的几种方法
    翻转二叉树力扣题目链接(opensnewwindow)翻转一棵二叉树。这道题目背后有一个让程序员心酸的故事,听说Homebrew的作者MaxHowell,就是因为没在白板上写出翻转二叉树,最......
  • POJ 1149 PIGS
       https://vjudge.net/problem/POJ-1149 #include<iostream>#include<queue>#include<cstring>#defineIOSstd::ios::sync_with_stdio(0)usingnamespace......
  • HashSet类
    Set接口的特点是无序和不重复上图就是无序的体现,HashSet底层数据结构是数组+链表,虽然数组有索引,但是数据并不是按索引来存放而是先通过Hash算法来给出各个数据存放的位......
  • LeetCode:两数相加II
    题目描述:给你四个整数数组nums1、nums2、nums3和nums4,数组长度都是n,请你计算有多少个元组(i,j,k,l)能满足:0<=i,j,k,l<nnums1[i]+nums2[j]+nums3[k......
  • 【LeetCode】28. 找出字符串中第一个匹配项的下标 -- 459. 重复的子字符串
    找出字符串中第一个匹配项的下标classSolution{public:intstrStr(stringhaystack,stringneedle){if(needle.size()==0)return0;ve......
  • LeetCode48. 旋转图像(/)
    原题解题目约束题解方法一classSolution{public:voidrotate(vector<vector<int>>&matrix){intn=matrix.size();//C++这里的......
  • leetcode 141. 环形链表
    哈希classSolution{public:boolhasCycle(ListNode*head){map<ListNode*,int>mp;while(head){if(mp[head]==1){......
  • leetcode 139. 单词拆分
    递归暴力超时#include<iostream>#include<vector>usingnamespacestd;classSolution{public:boolwordBreak(strings,vector<string>&wordDict){......
  • leetcode 16. 最接近的三数之和
    先排序,解决两数之和之后从i开始,解决i以后的tar-nums[i]的两数的最近和#include<bits/stdc++.h>usingnamespacestd;#definedebug(x)cout<<#x<<":"<<x<<endl;......