solution.json 2.5 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "35663731ec004bcb83bb0dddab7cec8e",
  "keywords": "哈希表,字符串,排序",
  "title": "字母异位词分组",
  "desc": [
    {
      "content": "\n<p>给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。</p>\n<p><strong>示例:</strong></p>\n<pre><strong>输入:</strong>[eat&quot;, &quot;tea&quot;, &quot;tan&quot;, &quot;ate&quot;, &quot;nat&quot;, &quot;bat&quot;]<strong><br />输出:</strong>[[ate&quot;,&quot;eat&quot;,&quot;tea&quot;],[&quot;nat&quot;,&quot;tan&quot;],[&quot;bat&quot;]]</pre>\n<p><strong>说明:</strong></p>\n<ul>\n<li>所有输入均为小写字母。</li>\n<li>不考虑答案输出的顺序。</li>\n</ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "\nimport java.util.*;\n\npublic class GroupAnagrams {\n\n\tpublic List<List<String>> groupAnagrams(String[] strs) {\n\t\tHashMap<String, ArrayList<String>> map = new HashMap<>();\n\t\tfor (String str : strs) {\n\t\t\tchar[] cs = str.toCharArray();\n\t\t\tArrays.sort(cs);\n\n\t\t\tString key = String.valueOf(cs);\n\t\t\tif (!map.containsKey(key)) {\n\t\t\t\tmap.put(key, new ArrayList<>());\n\t\t\t}\n\t\t\tmap.get(key).add(str);\n\t\t}\n\t\treturn new ArrayList(map.values());\n\t}\n\n\tpublic List<List<String>> groupAnagrams2(String[] strs) {\n\t\tif (strs.length <= 0) {\n\t\t\treturn new ArrayList<>();\n\t\t}\n\t\tHashMap<String, ArrayList<String>> map = new HashMap<>();\n\t\tfor (String str : strs) {\n\t\t\tchar[] cs = str.toCharArray();\n\t\t\tint[] count = new int[26];\n\t\t\tfor (char c : cs) {\n\t\t\t\t++count[c - 'a'];\n\t\t\t}\n\t\t\tStringBuilder s = new StringBuilder(\"\");\n\t\t\tfor (int num : count) {\n\t\t\t\ts.append(num);\n\t\t\t}\n\n\t\t\tString key = String.valueOf(s);\n\n\t\t\tif (!map.containsKey(key)) {\n\t\t\t\tmap.put(key, new ArrayList<>());\n\t\t\t}\n\t\t\tmap.get(key).add(str);\n\t\t}\n\t\treturn new ArrayList(map.values());\n\t}\n}\n",
    "language": "java"
  },
  "node_id": "dailycode-e782222cd8894664b3febf53492cbc11",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600470799"
}