给定一个字符串数组,将相同字谜组合在一起。(字谜是指颠倒字母顺序而成的字)
例如,给定 ["eat", "tea", "tan", "ate", "nat", "bat"],返回:
[
  ["ate", "eat","tea"],
  ["nat","tan"],
  ["bat"]
]
注意:所有的输入都是小写的。
详见:https://leetcode.com/problems/group-anagrams/description/

Java实现:

class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> res = new ArrayList<List<String>>();
int size = strs.length;
if(size<1){
return res;
}
Map<String,List<String>> map = new HashMap<String,List<String>>();
String tmp = "";
for(int i=0;i<size;i++){
tmp = strs[i];
char[] arrayOfString = tmp.toCharArray();
Arrays.sort(arrayOfString);
tmp = new String(arrayOfString);
if(map.containsKey(tmp)){
map.get(tmp).add(strs[i]);
}else{
List<String> item = new ArrayList<String>();
item.add(strs[i]);
map.put(tmp, item);
}
}
for (List<String> value : map.values()) {
res.add(value);
}
return res;
}
}

参考:https://www.cnblogs.com/grandyang/p/4385822.html

049 Group Anagrams 字谜分组的更多相关文章

  1. LeetCode 49: 字母异位词分组 Group Anagrams

    LeetCode 49: 字母异位词分组 Group Anagrams 题目: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. Given an array o ...

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

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

  3. [Swift]LeetCode49. 字母异位词分组 | Group Anagrams

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

  4. LeetCode第[49]题(Java):Group Anagrams

    题目:同字符分组 难度:Medium 题目内容: Given an array of strings, group anagrams together. 翻译:给定一组字符串数组,按相同字符组成的字符 ...

  5. [LeetCode] 249. Group Shifted Strings 分组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  6. LeetCode - 49. Group Anagrams

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

  7. Group Anagrams

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

  8. 【Leetcode】【Medium】Group Anagrams

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

  9. 49. Group Anagrams

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

随机推荐

  1. hdu1520树形dp入门

    题目链接 题意:要开派对,邀请了上司就不能邀请他的下属,邀请了下属就不能邀请他的上司,每个人有一个值,求邀请的人的总值最大 第一行给出一个数n,代表有n个人. 下面n行分别给出n个人的的值 再下面n行 ...

  2. linux命令学习笔记(52):ifconfig命令

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有 一个类似的工具,也就是ifconfig (interfaces config).通 ...

  3. [acm]HDOJ 2064 汉诺塔III

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=2064 汉诺塔变种,只能从中间专业,递归关系为:f(n)=3*f(n-1)+2. //汉诺塔变种,只能 ...

  4. CodeForces - 592D: Super M(虚树+树的直径)

    Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ s ...

  5. ACM学习历程—HDU 2795 Billboard(线段树)

    Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h ...

  6. AR/VR-VR-Info-Micron-Insight:一镜观一屋:VR 将建筑设计变为现实

    ylbtech-AR/VR-VR-Info-Micron-Insight:一镜观一屋:VR 将建筑设计变为现实 1.返回顶部 1. 一镜观一屋:VR 将建筑设计变为现实 想象一下,在一栋为你设计的还没 ...

  7. UDK游戏打包详解

    转自:http://blog.sina.com.cn/s/blog_944177030100ycki.html 安装完的udk目录下有4个主要的文件夹 Binaries -这个文件夹包含游戏的exe程 ...

  8. WPF win7+vs2010开发的打印功能,怎么在XP系统上无法打印

    在wpf 中打印功能很强大,但最近是在win7上可以但是布置到xp上就不可以了,查了好多资料终于知道怎么回事了原来xp里没有.net framework3.5 安装一个就OK了要先安装4.0.

  9. Excel添加水印

    Excel添加水印[源码下载] 步骤一:根据生成图片的类创建水印图片 步骤二: 代码在Excel中根据第一行获取sheet的列数[sheet.getRow(0).getLastCellNum() ], ...

  10. maven变量说明

    Maven内置变量说明: ${basedir} 项目根目录 ${project.build.directory} 构建目录,缺省为target ${project.build.outputDirect ...