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. A - 小希的迷宫

    来源 hdu1272 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是 ...

  2. phpcms v9 的表单向导功能的使用方法 附多个案例

    本文主要介绍phpcms v9的表单向导功能是如何使用的,并副多个案例讲解: 先介绍一下v9 的表单向导如何使用 表单向导做的很实用,生成一个表单,常用的是把它作为一个留言板,或者在招聘栏目作为一个供 ...

  3. logback logback.xml常用配置详解(一)<configuration> and <logger>

    logback logback.xml常用配置详解(一)<configuration> and <logger> 博客分类: Log java loglogback  原创文章 ...

  4. 转发一篇好文:36氪翻译自medium的文章: 读书没有 KPI:为什么坚持“一年读 100 本书”没用?

    你只是为了达成所谓的数量目标而读书. 编者按:读书本是一项安静.缓慢的活动,但随着现代社会节奏的加快,信息技术的广泛普及,读书这一行为模式也开始发生了变化.越来越多的人开始碎片化阅读,并且越来越多的文 ...

  5. 单目三维稠密重建方案:Quadtree-accelerated Real-time Monocular Dense Mapping

    论文:This is a monocular dense mapping system following the IEEE Robotics and Automation Letters (RA-L ...

  6. [No0000123]WPF DataGrid Columns Visibility的绑定

    场景:根据配置文件显示DataGrid中的某些列. 问题:Columns集合只是DataGrid的一个属性,这个集合在逻辑树或视觉树中是看不到的,也不会继承DataContext属性. 方法一:对Da ...

  7. ssm,dubbo框架搭建得配置文件

    在建立了parent,common,manager(pojo,mapper,sercie,web)的maven工程后,开始导入添加配置文件: pojo,mapper 最终会打成jar包,service ...

  8. [security] security engine things

    1. luarock luarock 之于 lua,就好比 pip 之于 python https://luarocks.org/ 2.  lua的库 [root@base package]# ls ...

  9. [maven] 初试maven

    环境 CentOS 6 一, 安装: [root@okk ~]# wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.0/binaries ...

  10. DDL触发器(用来控制用户的DDL行为)

    DDL触发器 禁止scott用户的所有DDL操作 create or replace trigger scott_forbid_trigger before ddl on schema begin r ...