solution.md 3.8 KB
Newer Older
1
# 字母异位词分组
F
fix bug  
feilong 已提交
2

3 4 5 6 7 8 9 10
<p>给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。</p>
<p><strong>示例:</strong></p>
<pre><strong>输入:</strong>[eat&quot;, &quot;tea&quot;, &quot;tan&quot;, &quot;ate&quot;, &quot;nat&quot;, &quot;bat&quot;]<strong><br />输出:</strong>[[ate&quot;,&quot;eat&quot;,&quot;tea&quot;],[&quot;nat&quot;,&quot;tan&quot;],[&quot;bat&quot;]]</pre>
<p><strong>说明:</strong></p>
<ul>
    <li>所有输入均为小写字母。</li>
    <li>不考虑答案输出的顺序。</li>
</ul>
每日一练社区's avatar
fix bug  
每日一练社区 已提交
11
<p>以下错误的选项是?</p>
F
fix bug  
feilong 已提交
12

13
## aop
F
fix bug  
feilong 已提交
14

15
### before
F
fix bug  
feilong 已提交
16

17 18 19 20 21
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
```cpp
int main()
{
    Solution sol;
    vector<string> strs = {"eat", "tea", "tan", "ate", "nat", "bat"};
    vector<vector<string>> res;

    res = sol.groupAnagrams(strs);

    for (auto i : res)
    {
        for (auto j : i)
            cout << j << " ";
        cout << endl;
    }
    return 0;
}
```

## 答案
F
fix bug  
feilong 已提交
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
```cpp
class Solution
{
public:
    vector<vector<string>> groupAnagrams(vector<string> &strs)
    {
        vector<vector<string>> res;
        map<string, int> kvmap;
        string s;
        int index = 0;

        for (int i = 0; i < strs.size(); i++)
        {
            s = strs[i];
            sort(s.begin(), s.end());
            if (kvmap.find(s) == kvmap.end())
            {
                index++;
                res.push_back(vector<string>{});
            }
            res[kvmap[s]].push_back(strs[i]);
        }
        return res;
    }
};
```
## 选项

F
fix bug  
feilong 已提交
72

73
### A
F
fix bug  
feilong 已提交
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
```cpp
class Solution
{
public:
    vector<vector<string>> groupAnagrams(vector<string> &strs)
    {
        vector<vector<string>> res;
        unordered_map<string, vector<string>> ht;
        for (const auto &str : strs)
        {
            int counts[26] = {0};
            for (char c : str)
            {
                counts[c - 'a']++;
            }
            string key;
            for (int i : counts)
            {
                key.push_back('#');
                key.push_back(i + '0');
            }
            ht[key].push_back(str);
        }
        for (const auto &t : ht)
        {
            res.push_back(t.second);
        }
        return res;
    }
};
```

### B
F
fix bug  
feilong 已提交
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
```cpp
class Solution
{
public:
    vector<vector<string>> groupAnagrams(vector<string> &strs)
    {
        map<string, vector<string>> anagram;
        vector<vector<string>> result;
        for (int i = 0; i < strs.size(); i++)
        {
            string str = strs[i];
            sort(str.begin(), str.end());
            if (anagram.find(str) == anagram.end())
            {
                std::vector<string> item;
                anagram[str] = item;
            }
            anagram[str].push_back(strs[i]);
        }
        map<string, vector<string>>::iterator it;
        for (it = anagram.begin(); it != anagram.end(); it++)
        {
            result.push_back((*it).second);
        }
        return result;
    }
};
```

### C
F
fix bug  
feilong 已提交
139

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
```cpp
class Solution
{
public:
    vector<vector<string>> groupAnagrams(vector<string> &strs)
    {
        int len = strs.size();
        if (len == 0)
            return {{""}};
        std::map<string, int> mymap;
        vector<vector<string>> ans;
        int cur = 0;
        for (int i = 0; i < len; i++)
        {
            string tmp(strs[i]);
            sort(tmp.begin(), tmp.end());
            std::map<string, int>::iterator it = mymap.find(tmp);
            if (it != mymap.end())
            {
                ans[it->second].push_back(strs[i]);
            }
            else
            {
                vector<string> item;
                item.push_back(strs[i]);
                ans.push_back(item);
                mymap.insert(make_pair(tmp, cur));
                cur++;
            }
        }
        return ans;
    }
};
```