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

思路


  这道题在一开始看到的时候,没有什么具体的思路。但是在想了一会之后觉得可以使用辅助字段来解决该问题。意思就是我们对列表中的字符串进行排序,然后将相同的添加进同一列表中。当遍历完毕之后得到最后的结果。时间复杂度为O(n mlog m),其中n为列表的长度,m为其中最长的字符串长度。空间复杂度为O(n*m)。

  在后面看到别人的解答之后学习到了另一种思路就是因为字符只有26个,所有我们设置一个列表,其中包含26个0,然后对列表中每一个字符串进行统计相应的字母出现的次数,然后把他加到相应的位置,然后将其转化为元祖并利用字典,将其对应的字符串添加到对应的列表中。时间复杂度复杂度为O(n*m), n为列表的长度,m为最长的单词数。空间复杂度为O(n*m)。

第一种思路的解决代码


 class Solution(object):
def groupAnagrams(self, strs):
ans = collections.defaultdict(list) # 辅助字典
for s in strs:
ans[tuple(sorted(s))].append(s) # 排序之后将其添加到对应的列表中
return ans.values() # 返回列表

第二种思路的解决代码


 class Solution(object):
def groupAnagrams(self, strs):
hmap = collections.defaultdict(list)
for st in strs:
array = [0] * 26 # 26个元素的列表
for l in st: # 将字符串元素计数
array[ord(l) - ord('a')] += 1
hmap[tuple(array)].append(st) # 按照元素出现的个数进行添加
return hmap.values()

【LeetCode每天一题】Group Anagrams(变位词组)的更多相关文章

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

    Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...

  2. LeetCode(49)Group Anagrams

    题目 Given an array of strings, group anagrams together. For example, given: ["eat", "t ...

  3. [LeetCode] 49. Group Anagrams 分组变位词

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  4. LeetCode第[49]题(Java):Group Anagrams

    题目:同字符分组 难度:Medium 题目内容: Given an array of strings, group anagrams together. 翻译:给定一组字符串数组,按相同字符组成的字符 ...

  5. LeetCode 49: 字母异位词分组 Group Anagrams

    LeetCode 49: 字母异位词分组 Group Anagrams 题目: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. Given an array o ...

  6. leetcode@ [49] Group Anagrams (Hashtable)

    https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...

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

    一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: [" ...

  8. [LeetCode] Group Anagrams 群组错位词

    Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...

  9. LeetCode OJ 49. Group Anagrams

    题目 Given an array of strings, group anagrams together. For example, given: ["eat", "t ...

随机推荐

  1. day_6.23py线程

    进程之间不能共享全局变量 线程之间共享全局变量,线程函数中的局部变量不是共享的!! #--*utf- 8*-- from threading import Thread import time g_n ...

  2. solus下 修改 crate-react-app 的默认下载依赖方式为 yarn

    vim /usr/local/share/.config/yarn/global/node_modules/create-react-app/createReactApp.js 或者是 修改 /usr ...

  3. Django----Admin流程

    Admin执行步骤 启动文件: 1:创建app-----stark 2:在每个app中创建stark 3:django----admin---- 4:在stark中写入:--------------- ...

  4. d7

    小数据池:int -5~256str 特殊字符,*数字20 ascii : 8位 1字节 表示1个字符unicode 32位 4个字节 表示一个字符utf- 8 1个英文 8位,1个字节 欧洲 16位 ...

  5. 网龙“MAD技术论坛”在榕举办 200余位技术人才共话“改变教育”

    9月16日,由网龙网络公司主办.msup协办的“MAD技术论坛”在榕举办,来自美国.香港.苏州等地的技术大牛受邀来到福州,围绕“Make a difference to education”这一论坛主 ...

  6. Linux NFS Root and PXE-Boot

    Linux NFS Root and PXE-Boot November 6, 2006 Home· Linux Linux kernel hacking and test running on th ...

  7. xcode工程编译错误:"An instance 0xca90200 of class UITableView was deallocated while key value observers were still registered with it"

    An instance 0xca90200 of class UITableView was deallocated while key value observers were still regi ...

  8. airflow 实战

    def print_hello(*a,**b): print a print "=========" print b print 'Hello world!' raise Valu ...

  9. No converter found for return value of type

    springMVC请求接口的时候报500  No converter found for return value of type 原因:这是因为springmvc默认是没有对象转换成json的转换器 ...

  10. vue获取地址栏传过来的参数VS原生js获取地址栏的参数

    Vue的方式 Vue的 query方式 ①this.$route.query.companyId ( companyId 为参数的名称 是$route 不是 $router) Vue的 params方 ...