49. Group Anagrams
来源:互联网 发布:淘宝网开店要交钱吗 编辑:程序博客网 时间:2024/05/01 22:35
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.
Subscribe to see which companies asked this question
以string为源生成key 相同字母不同排序的key相同 再以hashmap存就好
public class Solution { public List<List<String>> groupAnagrams(String[] strs) { List<List<String>> ret = new ArrayList<>(); if(strs==null||strs.length == 0)return ret; HashMap<String,List<String>>hmap = new HashMap<>(); for(int i = 0;i<strs.length;i++){ char [] chars = strs[i].toCharArray(); Arrays.sort(chars); String temp = new String(chars); if(!hmap.containsKey(temp)){ List<String> retList = new ArrayList<>(); retList.add(strs[i]); hmap.put(temp,retList); }else{ hmap.get(temp).add(strs[i]); } } Iterator<Map.Entry<String,List<String>>>iterator = hmap.entrySet().iterator(); while(iterator.hasNext()) { Map.Entry<String,List<String>> entry = iterator.next(); List<String> temp_list = entry.getValue(); Collections.sort(temp_list); ret.add(temp_list); } return ret; }}
0 0
- 49. Group Anagrams
- [leetcode] 49.Group Anagrams
- 49. Group Anagrams
- LeetCode 49. Group Anagrams
- [LeetCode]49. Group Anagrams
- leetcode 49. Group Anagrams
- 49. Group Anagrams LeetCode
- 49. Group Anagrams
- 49. Group Anagrams
- 49. Group Anagrams
- Leetcode 49. Group Anagrams
- 49. Group Anagrams
- LeetCode 49. Group Anagrams
- 49. Group Anagrams
- 49. Group Anagrams
- LeetCode 49. Group Anagrams
- [leetcode] 49. Group Anagrams
- LeetCode 49. Group Anagrams
- 文本编辑样式!
- JVM性能调优监控工具jps、jstack、jmap、jhat、jstat使用详解
- OKapi BM25 算法
- Storm集群组件和编程模型
- 带有Synchronized修饰的方法与普通方法在一个类中执行线程
- 49. Group Anagrams
- 310. Minimum Height Trees
- 简单的上下滚动
- mvp
- git向github上传完整项目
- 【bzoj3993】[SDOI2015]星际战争 二分+最大流
- 82. Remove Duplicates from Sorted List II
- OkHttp + Gson
- 黑科技:自定义百度云分享密码