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.

题目思路为利用collections.defualtdict() 去创建diction, 将相同模型的都append进去, 最后返回diction的所有values. 只是几个小细节要注意, 一个是diction不能以list作为key, 另外返回d.values()要加list.

class Solution:
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
#strs = set(strs) # 问面试官, 如果有duplicates如何处理
d = collections.defaultdict(list)
for s in strs:
template = [0]*26 #题目说只有lowcase
for c in s:
template[ord(c) - ord('a')] += 1
d[tuple(template)].append(s) # 注意加上tuple
return list(d.values()) # 注意加上list

Thanks for your reply about the Interview details. I've received the email and will follow the instructions in it. At same time, is it possible to set a phone call with you or Dan Corslund that you mentioned in your last email to discuss more about the preparation of the interviews except the coding part?

I am excited for the coming interviews and  see you all there.

Best regards,

Johnson

[Leetcode] 49. Group Anagrams_Medium的更多相关文章

  1. LeetCode - 49. Group Anagrams

    49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...

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

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

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

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

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

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

  5. LeetCode 49 Group Anagrams(字符串分组)

    题目链接: https://leetcode.com/problems/anagrams/?tab=Description   Problem:给一个字符串数组,将其中的每个字符串进行分组,要求每个分 ...

  6. leetcode 49 Group Anagram

    lc49 Group Anagram 逻辑很简单,就是统计字母出现次数,然后将完全相同的字符串放入同一list 关键是怎么实现 统计的部分,可以通过将string排序,Arrays.sort(),或者 ...

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

    是之前的重排列字符串的延伸,判断是重排列后存到HashMap中进行分组 这种HashMap进行分组的方式很常用 public List<List<String>> groupA ...

  8. [LeetCode] 249. Group Shifted Strings 分组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

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

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

随机推荐

  1. [OpenGL]用OpenGL制作动画

    //在窗口内绘制一个移动的矩形 /*我们通常还可以用OpenGL程序创建动画效果,这里我们利用前面的例子,绘制正方形,并使这个正方形在窗口的边框反弹.这里需要创建一个循环,在每次调用显示回调函数之前改 ...

  2. 【Spring源码分析系列】bean的加载

    前言 以 BeanFactory bf  = new XmlBeanFactory(new ClassPathResource("beans.xml"));为例查看bean的加载过 ...

  3. 原生js--文档加载时间

    onload触发时机:文档和所有的图片都加载完毕 DOMContentLoaded触发时机:文档加载并解析完毕,所有deferred脚本执行完毕.但此时图片和async脚本可能依旧在加载. ready ...

  4. MyBatis学习之多表查询

    一对多需求:即一张表class中又含有多张表(teacher,student)内容.现根据class_id 来获取对应的班级信息(包括学生和老师信息) 方式一:嵌套结果 使用嵌套结果映射来处理重复的联 ...

  5. 改变vux样式

    场景:修改 x-header 颜色 解决: 在创建文件路径如下 src/assets/less/theme.less ; 在build/webpack.base.conf.js下添加 这两行即可

  6. spring c3po 连接mysql,sqlserver

    1  连接mysql (1) http://wenku.it168.com/d_000096351.shtml 2  连接sqlserver (1)http://blog.csdn.net/vinep ...

  7. vue--使用过滤器

    有个项目需要可以添加和删除选项,但是左侧要显示的 A,B,C,D.... 解决思路: 循环的时候

  8. Redis 如何保持和MySQL数据一致

    1. MySQL持久化数据,Redis只读数据 redis在启动之后,从数据库加载数据. 读请求: 不要求强一致性的读请求,走redis,要求强一致性的直接从mysql读取 写请求: 数据首先都写到数 ...

  9. SharePoint 2013 字段属性之JSLink

    在SharePoint 2013中,SPField新增加了一个属性是JSLink,使用客户端脚本修改字段前台展示,我们可以用很多方法修改这个脚本的引用,然后来修改脚本,下面,我们举一个简单的例子. 具 ...

  10. PHP中有丰富的运算符集,它们中大部分直接来自于C语言

    PHP中有丰富的运算符集,它们中大部分直接来自于C语言.按照不同功能区分,运算符可以分为:算术运算符.字符串运算符.赋值运算符.位运算符.条件运算符,以及逻辑运算符等.当各种运算符在同一个表达式中时, ...