【LeetCode每天一题】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"] ]
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(变位词组)的更多相关文章
- [leetcode]49. Group Anagrams变位词归类
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- LeetCode(49)Group Anagrams
题目 Given an array of strings, group anagrams together. For example, given: ["eat", "t ...
- [LeetCode] 49. Group Anagrams 分组变位词
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- LeetCode第[49]题(Java):Group Anagrams
题目:同字符分组 难度:Medium 题目内容: Given an array of strings, group anagrams together. 翻译:给定一组字符串数组,按相同字符组成的字符 ...
- LeetCode 49: 字母异位词分组 Group Anagrams
LeetCode 49: 字母异位词分组 Group Anagrams 题目: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. Given an array o ...
- leetcode@ [49] Group Anagrams (Hashtable)
https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...
- 【一天一道LeetCode】#49. Group Anagrams
一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: [" ...
- [LeetCode] Group Anagrams 群组错位词
Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...
- LeetCode OJ 49. Group Anagrams
题目 Given an array of strings, group anagrams together. For example, given: ["eat", "t ...
随机推荐
- springboot集成rabbitmq的一些坑
一.默认管理页面地址是 http://127.0.0.1:15672 但是spring配置连接里面要把端口改成5672,如果不配置的话默认就是端口5672 spring.rabbitmq.host=1 ...
- ipv6禁用导致rpcbind服务启动失败解决办法
参考文档:http://blog.51cto.com/hld1992/2055028
- python中的os
import sys, os print(__file__) # 绝对路径,实际是文件名 /Users/majianyu/Desktop/test/bin/bin.py print(os.path.a ...
- CCPC-Wannafly Winter Camp Day5 Div1 - Sorting - [线段树]
题目链接:https://zhixincode.com/contest/22/problem/I?problem_id=314 样例输入 1 5 9 31 5 3 2 41 1 52 1 51 1 1 ...
- [No0000E2]Vmware虚拟机安装 苹果系统 mac OS 10.12
1.下载并安装Vmware:实验版本号:VMware-workstation-full-12.5.5-5234757:(忽略网上说的这个版本不行.可以装C盘,不过转C盘后后面都要用管理员权限运行其他软 ...
- block diagonal matrix 直和 块对角矩阵 不完美 有缺陷 缩放 射影几何
小结: 1.block diagonal matrix 直和 块对角矩阵 A block diagonal matrix is a block matrix that is a square mat ...
- 使用c#反射实现接口可视化调试页面
直接上代码,引用CommTools.dll.包括aspx显示页面和aspx.cs获取反射数据源代码 using System; using System.Collections.Generic; us ...
- iOS中Block的用法,举例,解析与底层原理(这可能是最详细的Block解析)
1. 前言 Block:带有自动变量(局部变量)的匿名函数.它是C语言的扩充功能.之所以是拓展,是因为C语言不允许存在这样匿名函数. 1.1 匿名函数 匿名函数是指不带函数名称函数.C语言中,函数是怎 ...
- [development][PCRE] old PCRE
介绍, man手册 txt版 http://www.pcre.org/original/pcre.txt html版 http://www.pcre.org/original/doc/html/pcr ...
- Web Deploy远程部署配置图解
原文链接:https://jingyan.baidu.com/album/642c9d34e614de644a46f783.html