LeetCode OJ:Group Anagrams(同字符字符群)

Given an array of strings, group anagrams together.

For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
Return:

[
  ["ate", "eat","tea"],
  ["nat","tan"],
  ["bat"]
]

找出同字母的字符群,比较简单的方法就是用map实现,将每个字符排下序作为key就可以了,代码如下:

 1 class Solution {
 2 public:
 3     vector<vector<string>> groupAnagrams(vector<string>& strs) {
 4         vector<vector<string>> ret;
 5         map<string, vector<string>> res;
 6         string tmp;
 7         for(int i = 0; i < strs.size(); ++i){
 8             tmp = strs[i];  //这里是关键
 9             sort(tmp.begin(), tmp.end());
10             res[tmp].push_back(strs[i]);
11         }
12         vector<string> tmpVec;
13         for(typename map<string, vector<string>>::iterator it = res.begin(); it != res.end(); ++it){
14             sort(it->second.begin(), it->second.end());
15         }
16         for(typename map<string, vector<string>>::iterator it = res.begin(); it != res.end(); ++it){
17             tmpVec.clear();
18             if(it->second.size() >= 1)
19                 for(int i = 0; i < it->second.size(); i++){
20                     tmpVec.push_back(it->second[i]);
21                 }
22             ret.push_back(tmpVec);
23         }
24         return ret;
25     }
26 };

还有很多种实现方法实际上,以后再来一一实现

时间: 2024-12-29 07:18:18

LeetCode OJ:Group Anagrams(同字符字符群)的相关文章

【Leetcode】Group Anagrams

题目链接:https://leetcode.com/problems/anagrams/ 题目: Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", &q

Leetcode 49. Group Anagrams

Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat",

LeetCode 49. Group Anagrams HashTable

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","

19.2.7 [LeetCode 49] Group Anagrams

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","

[leetcode]49. Group Anagrams变位词归类

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","

[leetcode]49. Group Anagrams重排列字符串分组

是之前的重排列字符串的延伸,判断是重排列后存到HashMap中进行分组 这种HashMap进行分组的方式很常用 public List<List<String>> groupAnagrams(String[] strs) { /* 其实重排列的那个题是有两种做法的,之前整理的是比较好的的一种字母哈希表的方法,还有一种方法 是把字符串排序,然后进行比较就行. 这个题正好就是用了排序的方法,排序后的字符串作为key,list作为value就行 一开始想用字母哈希表的方法,结果没做出来

【一天一道LeetCode】#49. Group Anagrams

一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"

【Leetcode】【Medium】Group Anagrams

Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat",

【LeetCode每天一题】Group Anagrams(变位词组)

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"] Output: [ ["ate","eat","tea"], ["nat","