[LeetCode] 49、字母异位词分组

题目描述

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

示例:

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

解题思路

关键是如何分组,利用sort(异位词可以变为相同的词)和unordered_map。直接看代码好理解

参考代码

排序 + 哈希表(会这个就行。。。)

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

        unordered_map<string, vector<string> > umap;
        for(auto str: strs){
            string temp = str;
            sort(temp.begin(), temp.end());
            umap[temp].push_back(str);
        }

        for(auto temp: umap)  // 注意这里
            res.push_back(temp.second);

        return res;
    }

};

按计数分组

public List<List<String>> groupAnagrams(String[] strs) {
    HashMap<String, List<String>> hash = new HashMap<>();
    for (int i = 0; i < strs.length; i++) {
        int[] num = new int[26];
        //记录每个字符的次数
        for (int j = 0; j < strs[i].length(); j++) {
            num[strs[i].charAt(j) - 'a']++;
        }
        //转成 0#2#2# 类似的形式
        String key = "";
        for (int j = 0; j < num.length; j++) {
            key = key + num[j] + '#';
        }
        if (hash.containsKey(key)) {
            hash.get(key).add(strs[i]);
        } else {
            List<String> temp = new ArrayList<String>();
            temp.add(strs[i]);
            hash.put(key, temp);
        }

    }
    return new ArrayList<List<String>>(hash.values());
}

质数相乘(效率更高)

class Solution {
public:
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        vector<vector<string>> res;
        unordered_map <double,vector<string> > m;
        double a[26]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101};
        for(string& s : strs)
        {
            double t = 1;
            for(char c : s)
            t *= a[c - 'a'];

            m[t].push_back(s);          //t为单词对应的质数乘积,m[t]则为该单词的异位词构成的vector
        }
        for(auto& n : m)                //n为键和值组成的pair
            res.push_back(n.second);
        return res;
    }
};

补充

计算前26个质数

#include<bits/stdc++.h>
using namespace std;

int main(){
    vector<int> ans;

    int curNum = 2;
    while(ans.size() < 26){
        bool flag = true;
        for(int i = 2; i <= sqrt(curNum); i++){
            if(curNum % i == 0){
                flag = false;
                break;
            }
        }

        if(flag)
            ans.push_back(curNum);
        curNum++;
    }

    for(auto num: ans){
        cout << num << ' ';
    }
    cout << "\n" << ans.size() << endl;

    return 0;
}

大数相乘(字符串相乘)

class Solution {
public:
    string multiply(string num1, string num2) {
        int length1 = num1.size();
        int length2 = num2.size();
        string res(length1 + length2, '0');  // 重要
        
        for(int j = length2-1; j >= 0; j--){
            for(int i = length1-1; i >= 0; i--){
                int tmp = (res[i+j+1] - '0' ) + (num2[j] - '0') * (num1[i] - '0');
                res[i+j+1] = tmp % 10 + '0';
                res[i+j] += tmp/10;  // 这里其实有可能 res[i+j] 所代表的数字大于'9',不过这对最终结果无影响。(比如举例:69*87 = 6003)
            }
        }
        
        for(int i = 0; i < length1+length2; i++){
            if(res[i] != '0')
                return res.substr(i);
        }
        
        return "0";
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值