Group Anagrams

时间:2021-12-31 01:16:02

Given an array of strings, group anagrams together.

For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
Return:

[
["ate", "eat","tea"],
["nat","tan"],
["bat"]
]
 public class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> map = new HashMap<String, List<String>>();
for(String str : strs){
// 将单词按字母排序
char[] carr = str.toCharArray();
Arrays.sort(carr);
String key = new String(carr);
List<String> list = map.get(key);
if(list == null){
list = new ArrayList<String>();
}
list.add(str);
map.put(key, list);
}
List<List<String>> res = new ArrayList<>();
// 将列表按单词排序
for(String key : map.keySet()){
List<String> curr = map.get(key);
Collections.sort(curr);
res.add(curr);
}
return res;
}
}

Group Anagrams的更多相关文章

  1. LeetCode - 49&period; Group Anagrams

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

  2. 【Leetcode】【Medium】Group Anagrams

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

  3. 49&period; Group Anagrams

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

  4. LeetCode49 Group Anagrams

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

  5. leetcode&commat; &lbrack;49&rsqb; Group Anagrams &lpar;Hashtable&rpar;

    https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...

  6. 【LeetCode】49&period; Group Anagrams

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

  7. 【一天一道LeetCode】&num;49&period; Group Anagrams

    一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: [" ...

  8. Group Anagrams 群组错位词

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

  9. &lbrack;Swift&rsqb;LeetCode49&period; 字母异位词分组 &vert; Group Anagrams

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

随机推荐

  1. Java正则速成秘籍(一)之招式篇

    导读 正则表达式是什么?有什么用? 正则表达式(Regular Expression)是一种文本规则,可以用来校验.查找.替换与规则匹配的文本. 又爱又恨的正则 正则表达式是一个强大的文本匹配工具,但 ...

  2. html5手写签名

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta co ...

  3. Oracle恢复删除数据 &amp&semi;&amp&semi; connect by 树形结构查询

    1.一个表中根据以父子级别关系查询显示出来(如图) select t.* from department t CONNECT BY PRIOR t.depid=t.supdepid ; --这样也可以 ...

  4. ADB not responding&period; You can wait more&comma;or kill"abd&period;exe" process manually and click 'Restart'

    在使用Android Studio进行开发的过程中,有时候编译运行时,会出现如下提示: ADB not responding. You can wait more,or kill"abd.e ...

  5. linux动态库默认搜索路径设置的三种方法

    众所周知, Linux 动态库的默认搜索路径是 /lib 和 /usr/lib .动态库被创建后,一般都复制到这两个目录中.当程序执行时需要某动态库, 并且该动态库还未加载到内存中,则系统会自动到这两 ...

  6. 【http】

    var qs = require('querystring') require('http').createServer(function(req, res) { //res.writeHead(20 ...

  7. Payssion,海外本地支付&lowbar;海外本地收款&lowbar;小语种本地支付&lowbar;外贸收款&lowbar;外贸网店收款&lowbar;欧洲本地支付&lowbar;俄罗斯本地支付&lowbar;巴西支付&lowbar;跨境支付&lowbar;PAYSSION,让跨境支付更轻松!

    Payssion,海外本地支付_海外本地收款_小语种本地支付_外贸收款_外贸网店收款_欧洲本地支付_俄罗斯本地支付_巴西支付_跨境支付_PAYSSION,让跨境支付更轻松!       首页 / 关于 ...

  8. JS-圣杯模式

    var inherit = (function(){ var F = function(){}; return function(Target,Origin){ F.prototype = Origi ...

  9. PHP中使用Redis长连接笔记

    pconnect函数声明 其中time_out表示客户端闲置多少秒后,就断开连接.函数连接成功返回true,失败返回false: pconnect(host, port, time_out, pers ...

  10. Python3&period;7 &plus; jupyter安装(CentOS6&period;5)

    Python3.7 + jupyter安装(CentOS6.5) 方法一(anaconda): anaconda是一个开源的Python发行版本 包含conda,python等大量的科学包以及依赖 优 ...