[leetcode]49. Group Anagrams变位词归类

Given an array of strings, group anagrams together.

Example:

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

题意:

给定一堆单词,把变位词放一块儿去。

碎碎念:

开始想说“eat” 转charArray {‘e‘, ‘a‘, ‘t‘}

“tea” 转charArray {‘t‘, ‘e‘, ‘a‘}

这样,我就错误的蜜汁以为以上charArray是相等的!

于是用一个Map<char[],  List<Integer>> map 来边扫input 边更新map。

为何要用List<Integer> ? 我蜜汁绕弯的想将index存下,最后再取出index对应的input string。 (自己都翻白眼啊!)

正确且高效的改进是,

sort “eat” 转charArray {‘e‘, ‘a‘, ‘t‘}  为字典排序 {‘a‘, ‘e‘, ‘t‘}

sort “tea” 转charArray {‘t‘, ‘e‘, ‘a‘}  为字典排序 {‘a‘, ‘e‘, ‘t‘}

Map<String,  List<String>> map 来存 <变位词sort后的同一结果,  各种可能的变位词>

Solution1:  HashMap

(1) convert each string to charArray, sort such charArray to make sure anagrams has uniform reference

(2) hashmap

(3) get all map.values()

code

/*
Time: O(n).  We traverse the input array
Space: O(n). We allocate a hashmap
*/
class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> result = new ArrayList<>();
        // corner case
        if(strs ==null) return result;

        Map<String,List<String>> map = new HashMap<>();   

        for(int i = 0; i< strs.length; i++){
            char [] curr = strs[i].toCharArray();
            Arrays.sort(curr);  // to make sure anagrams has uniform reference
            String key = String.valueOf(curr);
            if(!map.containsKey(key)){
                map.put(key,new ArrayList<String> ());
            }
            map.get(key).add(strs[i]);
        }
        for(List<String> list : map.values()){        //   可以直接简写成
            result.add(new ArrayList<>(list));       //        ||
        }                                           //         \/
        return result;                             //    return new ArrayList<>(map.values);
    }
}

原文地址:https://www.cnblogs.com/liuliu5151/p/10708256.html

时间: 2024-11-05 21:58:21

[leetcode]49. Group Anagrams变位词归类的相关文章

[虚拟机OA]Group Anagram 变位词归类

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","

Leetcode 49. Group Anagrams

Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat",

19.2.7 [LeetCode 49] Group Anagrams

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","

LeetCode 49. Group Anagrams HashTable

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","

[leetcode]49. Group Anagrams重排列字符串分组

是之前的重排列字符串的延伸,判断是重排列后存到HashMap中进行分组 这种HashMap进行分组的方式很常用 public List<List<String>> groupAnagrams(String[] strs) { /* 其实重排列的那个题是有两种做法的,之前整理的是比较好的的一种字母哈希表的方法,还有一种方法 是把字符串排序,然后进行比较就行. 这个题正好就是用了排序的方法,排序后的字符串作为key,list作为value就行 一开始想用字母哈希表的方法,结果没做出来

LeetCode开心刷题二十六天——49.Group Anagrams

49. Group Anagrams Medium 1824116FavoriteShare Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat

49. Group Anagrams(js)

49. Group Anagrams Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], [&q

【一天一道LeetCode】#49. Group Anagrams

一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"

【LeetCode】49. Group Anagrams

Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return: [ ["ate", "eat","tea"], ["nat",