Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
58 lines (47 loc) · 1.54 KB

File metadata and controls

58 lines (47 loc) · 1.54 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

difficulty: medium

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.

solution

language: java

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> resultList = new ArrayList<>();
        if (strs == null || strs.length == 0) {
            return resultList;
        }
        Map<String,List<String>> groupsMap = new HashMap<>();
        // 这里的 key 需要是 String,而不能使用数组,如果使用数组的话,key是内存地址,无法满足要求。
        // value 存储的是这个组的字符串的 index
        for (int i = 0; i < strs.length; i++) {
            String str = strs[i];
            char[] chars = str.toCharArray();
            Arrays.sort(chars);
            String s = String.valueOf(chars); // 排序后得到最新的字符串
            if (groupsMap.containsKey(s)) {
                groupsMap.get(s).add(str);
                continue;
            }
            List<String> resultItem = new ArrayList<>();
            groupsMap.put(s,resultItem);
            resultItem.add(str);
            resultList.add(resultItem);
        }
        return resultList;
    }
}

Morty Proxy This is a proxified and sanitized view of the page, visit original site.