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:

  1. For the return value, each inner list's elements must follow the lexicographic order.
  2. 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)的更多相关文章

  1. LeetCode - 49. Group Anagrams

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

  2. [LeetCode] 49. Group Anagrams 分组变位词

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

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

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

  4. LeetCode 49 Group Anagrams(字符串分组)

    题目链接: https://leetcode.com/problems/anagrams/?tab=Description   Problem:给一个字符串数组,将其中的每个字符串进行分组,要求每个分 ...

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

    是之前的重排列字符串的延伸,判断是重排列后存到HashMap中进行分组 这种HashMap进行分组的方式很常用 public List<List<String>> groupA ...

  6. 49. Group Anagrams - LeetCode

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

  7. 刷题49. Group Anagrams

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

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

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

  9. 【LeetCode】49. Group Anagrams 解题报告(Python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+hash 日期 题目地址:https://le ...

随机推荐

  1. uva 10791

    还算比较水的一个数学题 求因子的最小和  总是用小的数去除   注意特判  是用int不行哦........ #include <cstdio> #include <cmath> ...

  2. android下调试unity3d应用

    原地址:http://blog.csdn.net/armoonwei/article/details/7032455 目前貌似不支持断点调试,但可以通过日志打印(logcat)来跟踪. 在androi ...

  3. install Nagios on Unbuntu Unix

    Ubuntu Quickstart Up To: ContentsSee Also: Quickstart Installation Guides, Security Considerations I ...

  4. CF 136B Ternary Logic

    http://codeforces.com/problemset/problem/136/B 题意 :就是说tor是一个三进制的运算,代表的是两个三进制数的运算,两个三进制数按位逐一相加后对三取余,没 ...

  5. 179. Largest Number

    题目: Given a list of non negative integers, arrange them such that they form the largest number. For ...

  6. Android 解析XML

    public void getXML(String url) throws XmlPullParserException,IOException,URISyntaxException { String ...

  7. 远程唤醒 N54L

    远程唤醒 N54L 我的N54L装了一块买时带的WD500G黑盘,又装了一个WD2T绿盘,存些电影歌曲什么的,当NAS用,装的WIN7系统,长时间不使用就自动关机了,每次都得按一下电源开关键,于是就研 ...

  8. JSON格式转换成XML格式

    第一种方法: 需要使用命名空间System.Runtime.Serialization.Json 下面有JsonReaderWriterFactory XmlDictionaryReader read ...

  9. 【HDOJ】4366 Successor

    基本思路是将树形结构转换为线性结构.然后,所求即为一个区间内大于abi的最大的loy指向的ID.将结点按照abi降序排序,注意abi可能相等.然后,使用线段树单点更新,区间查询可解. /* 4366 ...

  10. linux系统中删除文件夹

    rm -rf 文件夹的名称 rm-r 文件名称