leetcode@ [49] Group Anagrams (Hashtable)
https://leetcode.com/problems/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"]
]
Note:
- For the return value, each inner list's elements must follow the lexicographic order.
- All inputs will be in lower-case.
class node {
public:
int idx;
string s;
node(int i, string str) {
idx =i;
s = str;
}
bool operator < (const node& rhs) {
if(s.compare(rhs.s) < || (s.compare(rhs.s) == && idx < rhs.idx)) return true;
return false;
}
};
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string> > res;
int n = strs.size();
if(n == ) return res;
if(n == ) {
vector<string> tmp;
tmp.push_back(strs[]);
res.push_back(tmp);
return res;
}
vector<vector<char> > vec(n);
vector<node> cs;
for(int i=; i<n; ++i) {
for(int j=; j<strs[i].length(); ++j) {
vec[i].push_back(strs[i][j]);
}
sort(vec[i].begin(), vec[i].end());
string ss = "";
for(int k=; k<vec[i].size(); ++k) {
ss += vec[i][k];
}
cs.push_back(node(i, ss));
}
sort(cs.begin(), cs.end());
int l = , r = l+;
while(r < n) {
while(r < n && (cs[l].s).compare(cs[r].s) == ) ++r;
vector<string> row;
for(int p=l; p<r; ++p) row.push_back(strs[cs[p].idx]);
res.push_back(row);
l = r; r = l+;
}
if(l < n) {
vector<string> row;
row.push_back(strs[cs[n-].idx]);
res.push_back(row);
}
for(int i=; i<res.size(); ++i) {
sort(res[i].begin(), res[i].end());
}
return res;
}
};
leetcode@ [49] Group Anagrams (Hashtable)的更多相关文章
- LeetCode - 49. Group Anagrams
49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...
- [LeetCode] 49. Group Anagrams 分组变位词
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- [leetcode]49. Group Anagrams变位词归类
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- LeetCode 49 Group Anagrams(字符串分组)
题目链接: https://leetcode.com/problems/anagrams/?tab=Description Problem:给一个字符串数组,将其中的每个字符串进行分组,要求每个分 ...
- [leetcode]49. Group Anagrams重排列字符串分组
是之前的重排列字符串的延伸,判断是重排列后存到HashMap中进行分组 这种HashMap进行分组的方式很常用 public List<List<String>> groupA ...
- 49. Group Anagrams - LeetCode
Question 49. Group Anagrams Solution 思路:维护一个map,key是输入数组中的字符串(根据字符排好序) Java实现: public List<List&l ...
- 刷题49. Group Anagrams
一.题目说明 题目是49. Group Anagrams,给定一列字符串,求同源词(包含相同字母的此)的集合.题目难度是Medium. 二.我的做法 题目简单,就不多说,直接上代码: class So ...
- 【一天一道LeetCode】#49. Group Anagrams
一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: [" ...
- 【LeetCode】49. Group Anagrams 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+hash 日期 题目地址:https://le ...
随机推荐
- Python的作用域
Python的作用域 转自:http://www.cnblogs.com/frydsh/archive/2012/08/12/2602100.html Python是静态作用域语言,尽管它自身是一个动 ...
- c++实现文本中英文单词和汉字字符的统计
源代码下载:http://download.csdn.net/detail/nuptboyzhb/4987141 1.统计文本中汉字的频数,为后续的文本分类做基础.对于汉字的统计,需要判断读取的是否为 ...
- Oracle sql查询
http://blog.csdn.net/jlds123/article/details/6572559
- HDU4651+数学公式
见Goolgle http://zh.wikipedia.org/zh-cn/%E6%95%B4%E6%95%B8%E5%88%86%E6%8B%86 /* 数学公式 ans[i]:i可以有ans[i ...
- jmeter 启用gzip压缩——解决测试中web服务器上行流量过大的问题
最近测了几个前端的项目,发现它们都有一个共同点:应用所在服务器的网卡上行(trans)非常大——经常是 117 MB/S,这已经逼近了千兆网卡的极限了.下面记录下排查和解决过程: 一. jmeter ...
- http://jingyan.baidu.com/article/e4511cf33479812b855eaf67.html
http://jingyan.baidu.com/article/e4511cf33479812b855eaf67.html
- js 中map的几种实现方式
简单使用 1, 通过对象方式: var myHash = {}; myHash["deviceID"] = "HY_000001"; myHash[" ...
- POJ2533——Longest Ordered Subsequence(简单的DP)
Longest Ordered Subsequence DescriptionA numeric sequence of ai is ordered if a1 < a2 < ... &l ...
- P44、面试题4:替换空格
题目:请实现一个函数,把字符串中的每个空格替换成“%20”.例如输入“We are happy.”,则输出“We%20are%20happy.”. 如果用java string类中提供的replace ...
- ssh-keygen的使用方法
一.概述 1.就是为了让两个linux机器之间使用ssh不需要用户名和密码.采用了数字签名RSA或者DSA来完成这个操作 2.模型分析 假设 A (192.168.20.59)为客户机器,B(192. ...