zoukankan      html  css  js  c++  java
  • [LeetCode] 49. 字母异位词分组

    题目链接 : https://leetcode-cn.com/problems/group-anagrams/

    题目描述:

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

    示例:

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

    说明:

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

    思路:

    这道题关键在于,如何找到可以唯一标识具有相同字母并且个数也一样的:

    思路一:单词按字典顺序排序

    思路二:用素数.


    关注我的知乎专栏,了解更多解题技巧,大家一起进步!

    代码:

    思路一

    class Solution:
        def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
            from collections import defaultdict
            lookup = defaultdict(list)
            for s in strs:
                lookup["".join(sorted(s))].append(s)
            return list(lookup.values())
    
    class Solution {
        public List<List<String>> groupAnagrams(String[] strs) {
            if (strs == null || strs.length ==0)  return new ArrayList<List<String>>();
            Map<String, List<String>> map= new HashMap<>();
            for (String str : strs) {
                char[] tmp = str.toCharArray();
                Arrays.sort(tmp);
                String keyStr = String.valueOf(tmp);
                if (! map.containsKey(keyStr)) map.put(keyStr,new ArrayList<String>());
                map.get(keyStr).add(str);
            }
            return new ArrayList<>(map.values());
            
        }
    }
    

    思路二

    class Solution:
        def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
            from collections import defaultdict
            prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103]
            lookup = defaultdict(list)
            for _str in strs:
                key_val = 1
                for s in _str:
                    key_val *= prime[ord(s) - 97]
                lookup[key_val].append(_str)
            return list(lookup.values())
    
    class Solution {
        public List<List<String>> groupAnagrams(String[] strs) {
            int[] prime = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103};
            Map<Integer,List<String>> map = new HashMap<>();
            for (String str : strs) {
                int key = 1;
                for(char c : str.toCharArray()){
                    key *= prime[c - 'a'];
                }
                if (!map.containsKey(key)) map.put(key, new ArrayList<String>());
                map.get(key).add(str);
            }
            return new ArrayList<>(map.values());
        }
    }
    
  • 相关阅读:
    ueditor精简插件和减少初次加载文件的方法
    The Art of Mocking
    What is a mocking framework? Why is it useful?
    黑盒测试、白盒测试、单元测试、集成测试、系统测试、验收测试的区别与联系
    What is the purpose of mock objects?
    What is Mocking?
    APPENDIX: How to apply the Apache License to your work
    开源 ≠ 免费,开源协议License详解
    如何选择开源许可证?
    五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT) – 整理
  • 原文地址:https://www.cnblogs.com/powercai/p/10876462.html
Copyright © 2011-2022 走看看