Skip to content

Latest commit

 

History

History
82 lines (60 loc) · 1.69 KB

solution_tmpl.md

File metadata and controls

82 lines (60 loc) · 1.69 KB

题目序号-难度-英文标题-中文标题

[力扣链接](力扣 URL)

Problems

49 Group Anagrams/字母异位词分组

CN

给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。

示例:

输入: ["eat", "tea", "tan", "ate", "nat", "bat"]
输出:

[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]

说明:

  • 所有输入均为小写字母。
  • 不考虑答案输出的顺序。

EN

Given an array of strings, group anagrams together.

Example:

Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]

Note:

  • All inputs will be in lowercase.
  • The order of your output does not matter.

解法

官方题解:leetcode-cn/字母异位词分组

解法一:排序数组分类

思路:找出过程中重复的数字避免进入无限循环

找出重复数字,就是解决题目的关键。

Java:

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        if (strs.length == 0) {
            return new ArrayList();
        }
        Map<String, List> result = new HashMap<String, List>();
        for (String s : strs) {
            char[] ca = s.toCharArray();
            Arrays.sort(ca);
            String key = String.valueOf(ca);
            if (!result.containsKey(key)) {
                result.put(key, new ArrayList());
            }
            result.get(key).add(s);
        }
        return new ArrayList(result.values());
    }
}