049 Group Anagrams 字谜分组
给定一个字符串数组,将相同字谜组合在一起。(字谜是指颠倒字母顺序而成的字)
例如,给定 ["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 字谜分组的更多相关文章
- LeetCode 49: 字母异位词分组 Group Anagrams
LeetCode 49: 字母异位词分组 Group Anagrams 题目: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. Given an array o ...
- [LeetCode] 49. Group Anagrams 分组变位词
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- [Swift]LeetCode49. 字母异位词分组 | Group Anagrams
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- LeetCode第[49]题(Java):Group Anagrams
题目:同字符分组 难度:Medium 题目内容: Given an array of strings, group anagrams together. 翻译:给定一组字符串数组,按相同字符组成的字符 ...
- [LeetCode] 249. Group Shifted Strings 分组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- LeetCode - 49. Group Anagrams
49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...
- Group Anagrams
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- 【Leetcode】【Medium】Group Anagrams
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- 49. Group Anagrams
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
随机推荐
- 菜鸟快速自学java00之变量类型
---恢复内容开始--- 菜鸟快速自学java00之变量类型 一.诉苦 自己成为了Java中的一只菜鸟,而且已经菜了好多天了,我为什么会这么菜?归根结底,还是觉得自己在累计知识的同时,没有做好笔记,导 ...
- L83
Kids Gulp 7 Trillion Calories Per Year Kids from the ages of 2 to 19, consume about seven trillion c ...
- leetcode 304. Range Sum Query 2D - Immutable(递推)
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 在旋转数组中找最小数
Add Date 2014-10-15 Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some p ...
- HDU-6035:Colorful Tree(虚树+DP)
这里有三道长得像的题: 一:HDU6036: There is a tree with nn nodes, each of which has a type of color represented ...
- zero to one (4)
复盘--天下武功唯快不破 There is no martial art is indefectible, while the fastest speed is the only way for lo ...
- 基于OpenCV的面部交换
需要装python库 OpenCV dlib docopt(根据打开方式选择是否装) # -*- coding: UTF-8 #本电脑试运行 命令 python F:\python_project\s ...
- liunx命令之:命令链接ftp服务器
1. 连接ftp服务器 格式:ftp [hostname| ip-address]a)在linux命令行下输入: ftp 192.168.1.1 b)服务器询问你用户名和密码,分别输入用户名和相应密码 ...
- ubuntu强制卸载软件
以卸载cups为例子 一:列出软件列表,找到需要卸载的软件的名字命令:dpkg --list
- ACM学习历程—HDU5396 Expression(递推 && 计数)
Problem Description Teacher Mai has n numbers a1,a2,⋯,an and n−1 operators("+", "-&qu ...