LeetCode Group Anagrams
来源:互联网 发布:基于tp框架的cms 编辑:程序博客网 时间:2024/06/05 00:13
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.
思路:用hashMap将anagram与对应的字符串序列关联起来。
代码如下:
class Solution{ public List<List<String>> groupAnagrams(String[] strs) { List<List<String>> ret = new ArrayList<List<String>>(); Map<String, List<String>> hm = new HashMap<String, List<String>>(); for (int i = 0; i < strs.length; i++) { String tmp = strs[i]; char[] chars = tmp.toCharArray(); Arrays.sort(chars); String sorttmp = new String(chars); if (hm.containsKey(sorttmp)) { hm.get(sorttmp).add(tmp); } else { List<String> list_str = new ArrayList<String>(); list_str.add(tmp); hm.put(sorttmp, list_str); } } for (String str : hm.keySet()) { List<String> value = hm.get(str); Collections.sort(value); ret.add(hm.get(str)); } return ret; }}
0 0
- LeetCode Group Anagrams
- Leetcode#49||Group Anagrams
- [LeetCode] Group Anagrams
- [leetcode] 49.Group Anagrams
- LeetCode题解:Group Anagrams
- [leetcode] Group Anagrams
- LeetCode-Group Anagrams
- LeetCode OJ:Group Anagrams
- [Leetcode]Group Anagrams
- 《leetCode》:Group Anagrams
- leetcode | Group Anagrams
- leetcode 49:Group Anagrams
- LeetCode -- Group Anagrams
- LeetCode---Group Anagrams
- leetcode Group Anagrams
- Leetcode 49: Group Anagrams
- LeetCode Group Anagrams
- LeetCode 049 Group Anagrams
- iOS对耗时操作的处理方法
- java.lang.NoClassDefFoundError: Could not initialize class java.awt.Toolkit异常的解决方法
- 浅谈决策树算法以及matlab实现ID3算法
- Path Sum II
- android创建多线程的方法
- LeetCode Group Anagrams
- java中的内部类总结
- Linux下的MYSQL主主复制
- UI AutoLayer 自动布局-->约束
- Error: Cannot find configuration directory: /etc/hadoop
- MySQL 性能优化
- 初学hibernate笔记
- PAT-1048 数字加密20
- ORA-02095: specified initialization parameter cannot be modified