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 ...
随机推荐
- hdu 4192
dfs全排列 加 模拟计算 #include <iostream> #include <cstdio> #include <cstdlib> #include ...
- 20130729--Samba的学习
(一).基本概念 samba是一个能让你的Unix计算机和其它MS Windows计算机相互共享资源的软件. samba提供有关资源共享的三个功能,包括:smbd,执行它可以使Unix能够共享资源给其 ...
- Android 文字链接 文字点击时的背景颜色
案例:实现“忘记密码?”这个链接,并且在按下的时候改变颜色. 方法一:这个可以用TextView实现: 主界面main.xml: <?xml version="1.0" en ...
- Android:自定义适配器
无论是ArrayAdapter还是SimpleAdapter都继承了BaseAdapter,自定义适配器同样继承BaseAdapter 实例:Gallery实现图片浏览器 <?xml versi ...
- C#字符串的常用操作
一.string关键字与StringBuilder类 C# 字符串是使用 string 关键字声明的一个字符数组.字符串是使用引号声明的,如下例所示: string s = "Hello, ...
- 在C++中调用DLL中的函数
如何在C++中调用DLL中的函数 应用程序使用DLL可以采用两种方式:一种是隐式链接,另一种是显式链接.在使用DLL之前首先要知道DLL中函数的结构信息.Visual C++6.0在VC\bin目录下 ...
- C#中的预处理器指令
C#中有许多名为“预处理器指令”的命令.这些命令从来不会转化为可执行代码中的命令,但会影响编译过程的各个方面. 例如,使用预处理器指令可以禁止编译器编译代码的某一部分.如果计划发布两个版本的代码,即基 ...
- CodeForces Round #290 Div.2
A. Fox And Snake 代码可能有点挫,但能够快速A掉就够了. #include <cstdio> int main() { //freopen("in.txt&quo ...
- I.MX6 Linux 自动获取AR1020 event input节点
/*********************************************************************** * I.MX6 Linux 自动获取AR1020 ev ...
- Java [Leetcode 43]Multiply Strings
题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...