您的位置:首页 > 其它

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: