leetcode--Anagrams
2015-06-06 16:10
232 查看
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.public class Solution {
public List<String> anagrams(String[] strs) {
List<String> res = new ArrayList<String>();
HashMap<String,Integer> map = new HashMap<String,Integer>();
for(int i=0;i<strs.length;i++){
char[] c = strs[i].toCharArray();
Arrays.sort(c);
String t = new String(c);
if(map.containsKey(t)){
if(!res.contains(strs[map.get(t)])){
res.add(strs[map.get(t)]);
}
res.add(strs[i]);
}else{
map.put(t, i);
}
}
return res;
}
}
Note: All inputs will be in lower-case.public class Solution {
public List<String> anagrams(String[] strs) {
List<String> res = new ArrayList<String>();
HashMap<String,Integer> map = new HashMap<String,Integer>();
for(int i=0;i<strs.length;i++){
char[] c = strs[i].toCharArray();
Arrays.sort(c);
String t = new String(c);
if(map.containsKey(t)){
if(!res.contains(strs[map.get(t)])){
res.add(strs[map.get(t)]);
}
res.add(strs[i]);
}else{
map.put(t, i);
}
}
return res;
}
}
相关文章推荐
- TechParty-IOS与iwatch专题沙龙笔记
- VTK轮廓提取
- 第十四周项目1(4)-小玩文件
- 02两栈共享空间_DoubleStack--(栈与队列)
- 关于 AppDelegate 、UIApplication 简单的用法
- 算法导论实验四_分治法求平面上的最小点对
- 每日一题F
- 黑马程序员——Java基础---集合(Collection和List)
- 01顺序栈_Stack---(栈与队列)
- Javascript String类的属性及方法
- 删除任务栏右下角Windows 10推送更新图标
- Android中Bitmap和Drawable
- MSP430 G2553 LaunchPad设置GPIO
- LAMP+LVS+KEEPALIVED(二)
- vi常用命令操作
- 优质题表(机密版)
- 03静态链表_StaticLinkList--(线性表)
- 算法导论实验三_区间树查找
- 使用Xshell连接Ubuntu
- 02线性表链式存储_LinkList--(线性表)