Given an array of strings, group anagrams together.

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

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

Note:

  1. For the return value, each inner list's elements must follow the lexicographic order.
  2. All inputs will be in lower-case.

思路:

anagram: 由颠倒字母而成的单词

anagram无关乎字母出现的顺序,所以将字母排序后再比较每个字母出现的次数

class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
if (strs.size() <= ) return result; vector<vector<string>> result;
map<string,int> anagram;
int resIndex = ;
string s; for (int i = ; i < strs.size(); ++i)
{
s = strs[i];
sort(s.begin(), s.end()); //anagram无关乎字母顺序,所以将string按字母排序后再比较
if (anagram.find(s) == anagram.end()) { //如果还没有出现过该anagram
vector<string> item;
item.push_back(strs[i]);
result.push_back(item);
anagram.insert(make_pair(s, resIndex++));
} else {
result[anagram[s]].push_back(strs[i]);
}
} //each inner list's elements must follow the lexicographic order
for(int i = ; i < result.size(); i++){
sort(result[i].begin(), result[i].end());
}
return result;
}
};

49. Group Anagrams (string, HashTable)的更多相关文章

  1. LeetCode - 49. Group Anagrams

    49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...

  2. 刷题49. Group Anagrams

    一.题目说明 题目是49. Group Anagrams,给定一列字符串,求同源词(包含相同字母的此)的集合.题目难度是Medium. 二.我的做法 题目简单,就不多说,直接上代码: class So ...

  3. 49. Group Anagrams - LeetCode

    Question 49. Group Anagrams Solution 思路:维护一个map,key是输入数组中的字符串(根据字符排好序) Java实现: public List<List&l ...

  4. leetcode@ [49] Group Anagrams (Hashtable)

    https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...

  5. 49. Group Anagrams

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  6. 【LeetCode】49. Group Anagrams

    题目: Given an array of strings, group anagrams together. For example, given: ["eat", " ...

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

    一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: [" ...

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

    Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...

  9. LeetCode OJ 49. Group Anagrams

    题目 Given an array of strings, group anagrams together. For example, given: ["eat", "t ...

随机推荐

  1. icon fonts入门

    iconfont网站 http://www.iconfont.cn(推荐) http://fontello.com/ http://fontawesome.io/   https://icomoon. ...

  2. PostgreSQL逻辑复制槽

    Schema | Name | Result data type | Argument data types | Type ------------+------------------------- ...

  3. Windows环境下 PyQt5 如何安装MySql驱动 (PyQt5连接MYSQL时显示Driver not loaded解决方案)

    参考文章: https://blog.csdn.net/qq_38198744/article/details/80261695 前文说过如何在Ubuntu环境下 为PyQt5  安装MySql驱动, ...

  4. Linux 环境下 javac 编译错误: 编码UTF8的不可映射字符 (编码UTF8/GBK的不可映射字符)

    Linux 系统下一般默认使用UTF-8编码, 使用javac 编辑使用其他编码格式编写的源吗时,会出现  “ 错误: 编码UTF8的不可映射字符 ”. 最近在使用  javac 编译 一个在wind ...

  5. backports移植rtlwifi驱动

    /************************************************************************ * backports移植rtlwifi驱动 * 说 ...

  6. javax.el.PropertyNotFoundException: Property 'imgUrl' not found on type java.lang.String

    严重: Servlet.service() for servlet jsp threw exception javax.el.PropertyNotFoundException: Property ' ...

  7. pthread中向线程发送信号(pthread_kill )

    pthread_kill 语法 int pthread_kill(thread_t tid, int sig); #include <pthread.h> #include <sig ...

  8. PostgreSQL 9.6 keepalived主从部署

    ## 环境: PostgreSQL版:9.6 角色                     OS                    IPmaster                 CentOS7 ...

  9. CCFlow SDK模式开发(有比较详细的代码,以服务的形式与ccflow数据库进行数据交互)

    http://www.cnblogs.com/s0611163/p/3963142.html 需求: 1.业务数据要保存在我们自己的数据库里     2.CCFlow有保存草稿的功能,但是领导要求每个 ...

  10. win7下openvpn不能自动加路由

    在win7下用openvpn一直报这个错误,配置文件里的路由一直加不上,但是可以拔得上服务器,只好手工加路由.Thu Apr 07 23:13:51 2011 Notified TAP-Win32 d ...