力扣 题目49--字母异位词分组

题目

 

题解

代码1

使用multiset容器 multiset可以自动排序 

1 multiset<int> transformation(string& str) {
2     multiset<int> res;
3     for (int i = 0; i < str.length(); i++) {
4         res.insert(str[i]);
5     }
6     return res;
7 }

得到的res去和保存之前所有res的ress比较  如果有相同的得到下标 然后再去在结果集里按照下标push_back str即可 如果没有则把得到的res加入ress 在结果集的最后新开空间加入str

1 vector<multiset<int>> ress; 

但是这样空间复杂度与时间复杂度都很高

代码2

在代码1使用了multiset 能不能用另一种方法取代呢?

我们可以使用

1 vector<int> transformation(string &str) {
2     vector<int> res(26,0);
3     for (int i = 0; i < str.length(); i++) {
4          res[str[i] - 97]++;
5     }
6     return res;
7 }

即让26个字母去取代 然后比较 但是这样空间复杂度与时间复杂度也很高

代码3

不用容器 直接sort排序 也是可以的 

 1 vector<string> res;
 2         vector<vector<string>> result;
 3         for (int i = 0; i < strs.size(); i++) {
 4             string replace = strs[i];
 5             sort(replace.begin(), replace.end());
 6             bool add = false;
 7             for (int j = 0; j < res.size(); j++) {
 8                 if (result[j][0].size() == strs[i].size() && replace == res[j]) {
 9                     result[j].push_back(strs[i]);
10                     add = true;
11                     break;
12                 }
13             }
14             if (!add) {
15                 vector<string> str = { strs[i] };
16                 result.push_back(str);
17                 res.push_back(replace);
18             }
19         }
20         return result;

空间复杂度与时间复杂度少了很多

代码4 

复杂度其实和查找关系比较大 那么有没有容器查找简单 那就是Map 而且Map 其实按照 key(排序后的str) 添加之后发现second和我们结果集一模一样  转换一下即可

 1 map<string, vector<string>> Map;
 2         vector<vector<string>> result;
 3         for (int i = 0; i < strs.size(); i++) {
 4             string replace = strs[i];
 5             sort(replace.begin(), replace.end());
 6             Map[replace].push_back(strs[i]);
 7         }
 8         for (auto ite = Map.begin(); ite != Map.end(); ite++) {
 9              result.push_back(ite->second);
10         }
11         return result;

代码5 如果能找到一种方法代替代码4中的key那么 复杂度可以更少 在力扣评论区中有这样一种思路 使用质数 代替26个字母 因为相乘的结果是唯一的

 1 vector<int> Prime = { 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 };
 2 class Solution {
 3 public:
 4     vector<vector<string>> groupAnagrams(vector<string>& strs) {
 5         map<double, vector<string>> Map;
 6         vector<vector<string>> result;
 7         for (int i = 0; i < strs.size(); i++) {
 8             double num = 1;
 9             for (int j = 0; j < strs[i].size(); j++) {
10                 num *= Prime[strs[i][j] - 97];
11             }
12             Map[num].push_back(strs[i]);
13         }
14         for (auto ite = Map.begin(); ite != Map.end(); ite++) {
15              result.push_back(ite->second);
16         }
17         return result;
18     }
19 };

完整代码


代码1

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 #include <set>
 5 using namespace std;
 6 multiset<int> transformation(string& str) {
 7     multiset<int> res;
 8     for (int i = 0; i < str.length(); i++) {
 9         res.insert(str[i]);
10     }
11     return res;
12 }
13 
14 class Solution {
15 public:
16     vector<vector<string>> groupAnagrams(vector<string>& strs) {
17         vector<multiset<int>> res;
18         vector<vector<string>> result;
19         for (int i = 0; i < strs.size(); i++) {
20             multiset<int> strvector = transformation(strs[i]);
21             bool add = false;
22             for (int j = 0; j < res.size(); j++) {
23                 if (result[j][0].size() == strs[i].size() && strvector == res[j]) {
24                     result[j].push_back(strs[i]);
25                     add = true;
26                     break;
27                 }
28             }
29             if (!add) {
30                 vector<string> str = { strs[i] };
31                 result.push_back(str);
32                 res.push_back(strvector);
33             }
34         }
35         return result;
36     }
37 };
38 
39 int main() {
40     Solution sol;
41     vector<string> strs = { "aaa", "aaaa", "aaac", "acaa" };
42     vector<vector<string>> result = sol.groupAnagrams(strs);
43     for (int i = 0; i < result.size(); i++) {
44         for (int j = 0; j < result[i].size(); j++) {
45             cout << result[i][j] << " ";
46         }
47         cout << endl;
48     }
49 }
View Code

代码2

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 #include <set>
 5 using namespace std;
 6 //1.开辟数组
 7 //2.统计字母个数
 8 //3.比较数组
 9 vector<int> transformation(string &str) {
10     vector<int> res(26,0);
11     for (int i = 0; i < str.length(); i++) {
12          res[str[i] - 97]++;
13     }
14     return res;
15 }
16 class Solution {
17 public:
18     vector<vector<string>> groupAnagrams(vector<string>& strs) {
19         vector<vector<int>> res;
20         vector<vector<string>> result;
21         for (int i = 0; i < strs.size(); i++) {
22             vector<int> strvector = transformation(strs[i]);
23             bool add = false;
24             for (int j = 0; j < res.size(); j++) {
25                 if (result[j][0].size() == strs[i].size() && strvector == res[j]) {
26                     result[j].push_back(strs[i]);
27                     add = true;
28                     break;
29                 }
30             }
31             if (!add) {
32                 vector<string> str = { strs[i] };
33                 result.push_back(str);
34                 res.push_back(strvector);
35             }
36         }
37         return result;
38     }
39 };
40 
41 int main() {
42     Solution sol;
43     vector<string> strs = { "aaa", "aaaa", "aaac", "acaa"};
44     vector<vector<string>> result= sol.groupAnagrams(strs);
45     for (int i = 0; i < result.size(); i++) {
46         for (int j = 0; j < result[i].size(); j++) {
47             cout << result[i][j]<<" ";
48         }
49         cout << endl;
50     }
51 }
View Code

代码3

 1 class Solution {
 2 public:
 3     vector<vector<string>> groupAnagrams(vector<string>& strs) {
 4         vector<string> res;
 5         vector<vector<string>> result;
 6         for (int i = 0; i < strs.size(); i++) {
 7             string replace = strs[i];
 8             sort(replace.begin(), replace.end());
 9             bool add = false;
10             for (int j = 0; j < res.size(); j++) {
11                 if (result[j][0].size() == strs[i].size() && replace == res[j]) {
12                     result[j].push_back(strs[i]);
13                     add = true;
14                     break;
15                 }
16             }
17             if (!add) {
18                 vector<string> str = { strs[i] };
19                 result.push_back(str);
20                 res.push_back(replace);
21             }
22         }
23         return result;
24     }
25 };
View Code

代码4

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 #include<algorithm>
 5 #include<map>
 6 using namespace std;
 7 class Solution {
 8 public:
 9     vector<vector<string>> groupAnagrams(vector<string>& strs) {
10         map<string, vector<string>> Map;
11         vector<vector<string>> result;
12         for (int i = 0; i < strs.size(); i++) {
13             string replace = strs[i];
14             sort(replace.begin(), replace.end());
15             Map[replace].push_back(strs[i]);
16         }
17         for (auto ite = Map.begin(); ite != Map.end(); ite++) {
18              result.push_back(ite->second);
19         }
20         return result;
21     }
22 };
23 
24 int main() {
25     Solution sol;
26     vector<string> strs = { "aaa", "aaaa", "aaac", "acaa" };
27     vector<vector<string>> result = sol.groupAnagrams(strs);
28     for (int i = 0; i < result.size(); i++) {
29         for (int j = 0; j < result[i].size(); j++) {
30             cout << result[i][j] << " ";
31         }
32         cout << endl;
33     }
34 }
View Code

代码5

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 #include<algorithm>
 5 #include<map>
 6 using namespace std;
 7 vector<int> Prime = { 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 };
 8 class Solution {
 9 public:
10     vector<vector<string>> groupAnagrams(vector<string>& strs) {
11         map<double, vector<string>> Map;
12         vector<vector<string>> result;
13         for (int i = 0; i < strs.size(); i++) {
14             double num = 1;
15             for (int j = 0; j < strs[i].size(); j++) {
16                 num *= Prime[strs[i][j] - 97];
17             }
18             Map[num].push_back(strs[i]);
19         }
20         for (auto ite = Map.begin(); ite != Map.end(); ite++) {
21              result.push_back(ite->second);
22         }
23         return result;
24     }
25 };
26 
27 int main() {
28     Solution sol;
29     vector<string> strs = { "aaa", "aaaa", "aaac", "acaa" };
30     vector<vector<string>> result = sol.groupAnagrams(strs);
31     for (int i = 0; i < result.size(); i++) {
32         for (int j = 0; j < result[i].size(); j++) {
33             cout << result[i][j] << " ";
34         }
35         cout << endl;
36     }
37 }
View Code

 

posted @ 2022-05-12 12:52  无聊的阿库娅  阅读(35)  评论(0编辑  收藏  举报