zoukankan      html  css  js  c++  java
  • 力扣算法题—049字母异位分组

      1 #include "000库函数.h"
      2 
      3 
      4 //笨方法,循环,遍历
      5 //先将每个字母进行遍历,并给排序,这样就可以确定出有几种字符串,然后就可以开辟空间了
      6 //然后给每个字符串进行找位子
      7 //笨方法,时间久 864ms  ,内存大17.4M
      8 class Solution {
      9 public:
     10     vector<vector<string>> groupAnagrams(vector<string>& strs) {
     11         vector<vector<string>>Res;
     12         vector<string>st;
     13         set<string>s;        
     14         for (auto a : strs) {
     15             sort(a.begin(), a.end());//给每个字符排序
     16             s.insert(a);//去除相同字母的组合
     17         }                
     18         st.assign(s.begin(), s.end());//将不相同的字符串储存下来
     19         Res.resize(s.size());//给Res开辟空间
     20 
     21         for (auto a : strs) {
     22             auto b = a;
     23             sort(b.begin(), b.end());//给每个字符排序
     24             for (int i = 0; i < st.size(); ++i)
     25                 if (st[i] == b)
     26                     Res[i].push_back(a);        
     27         }
     28         return Res;
     29 
     30     }
     31 };
     32 
     33 //思路和上面一样,代码简便点,减少了循环次数
     34 class Solution {
     35 public:
     36     vector<vector<string>> groupAnagrams(vector<string>& strs) {
     37         vector<vector<string>> res;
     38         unordered_map<string, int> mp;
     39         for (int i = 0, j = 0; i < strs.size(); i++) {
     40             string st = strs[i];
     41             sort(st.begin(), st.end());
     42             if (mp.find(st) != mp.end()) {
     43                 res[mp[st]].push_back(strs[i]);
     44             }
     45             else {
     46                 vector<string> tmp{ strs[i] };
     47                 res.push_back(tmp);
     48                 mp[st] = j++;
     49             }
     50         }
     51         return res;
     52     }
     53 };
     54 
     55 //用字典最好
     56 class Solution {
     57 public:
     58     vector<vector<string>> groupAnagrams(vector<string>& strs) {
     59         vector<vector<string>> res;
     60         unordered_map<string, vector<string>> m;
     61         for (string str : strs) {
     62             string t = str;
     63             sort(t.begin(), t.end());
     64             m[t].push_back(str);
     65         }
     66         for (auto a : m) {
     67             res.push_back(a.second);
     68         }
     69         return res;
     70     }
     71 };
     72 
     73 
     74 //下面这种解法没有用到排序,我们用一个大小为26的int数组来统计每个单词中字符出现的次数,
     75 //然后将int数组转为一个唯一的字符串,跟字符串数组进行映射,这样我们就不用给字符串排序了,
     76 //代码如下:
     77 class Solution {
     78 public:
     79     vector<vector<string>> groupAnagrams(vector<string>& strs) {
     80         vector<vector<string>> res;
     81         unordered_map<string, vector<string>> m;
     82         for (string str : strs) {
     83             vector<int> cnt(26, 0);
     84             string t = "";
     85             for (char c : str) ++cnt[c - 'a'];
     86             for (int d : cnt) t += to_string(d) + "/";//将数字转换为字母,与相面思想类似,t即为排了序的字符串
     87             m[t].push_back(str);
     88         }
     89         for (auto a : m) {
     90             res.push_back(a.second);
     91         }
     92         return res;
     93     }
     94 };
     95 
     96 void T049() {
     97     Solution s;
     98     vector<vector<string>>Res;
     99     vector<string>v;
    100     v = { "eat", "tea", "tan", "ate", "nat", "bat" };
    101     Res = s.groupAnagrams(v);
    102     for (auto &a : Res) {
    103         for (auto b : a)
    104             cout << b << "  ";
    105         cout << endl;
    106     }
    107 }
  • 相关阅读:
    Redis-安装
    Redis-介绍
    Redis 教程(转)
    C# Redis 帮助类
    sublime text3---Emmet:HTML/CSS代码快速编写神器
    Sublime Text3 Package Control和Emmet插件安装方法
    vs2010音频文件压缩 调用lame_enc.dll将WAV格式转换成MP3
    vs学习过程中遇见的各种问题
    vs2010中添加dll文件
    解决angular11打包报错Type 'Event' is missing the following properties from type 'any[]': ...Type 'Event' is not assignable to type 'string'
  • 原文地址:https://www.cnblogs.com/zzw1024/p/10615673.html
Copyright © 2011-2022 走看看