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. 五、K3 WISE 开发插件《K3 Wise 群发短信配置开发(一)之短信平台配置》

    开发环境:K/3 Wise 13.0 目录 一.创建短信数据库 二.配置短信接口 三.设置帐套关键字 四.查询短信余额 一.创建短信数据库 打开帐套管理: 账号默认为Admin,密码不填: 菜单“系统 ...

  2. 利用腾讯云COS云对象存储定时远程备份网站

    版权声明:本文由张戈 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/942851001487125915 来源:腾云阁 h ...

  3. Android NDK学习(2)Windows下NDK开发环境配置

    转:http://www.cnblogs.com/fww330666557/archive/2012/12/14/2817386.html 一.配置好Android开发环境 二.下载安装安卓NDK   ...

  4. 题目1457:非常可乐(广度优先遍历BFS)

    题目链接:http://ac.jobdu.com/problem.php?pid=1457 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  5. magent实现memcached集群的一个问题

    之前我们小组封装了一个memcached类库,里面有一个名为RemoveStartWith的方法可以根据起始字符串删除所有节点中负责键值规则的缓存项.它实现的原理就是通过stats命令获取每个节点的所 ...

  6. 【转】Openstack中oslo_config模块学习

    OpenStack的项目貌似越来越多了,在Grizzly版之前,每个项目都得实现一套处理配置文件的代码.在每个项目的源码中基本上都可以找到openstack/common/cfg.py,inipars ...

  7. Android 模糊效果 FastBlur

    import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; impor ...

  8. ftok函数

    ftok函数 系统建立IPC通讯(消息队列.信号量和共享内存)时必须指定一个ID值.通常情况下,该id值通过ftok函数得到. ftok原型 头文件: #include <sys/types.h ...

  9. vue--父子组件的传值

    什么是父子组件? 组件中引入组件,被引入的组件就是子组件.例如在 Hello.vue 组件中引入 Header.vue 组件,那么 Hello.vue 就是父组件,Header.vue就是子组件. 一 ...

  10. Centos6.5 虚拟机Mongodb创建副本集

    简单副本集的搭建 官方demo的最小化的副本集为Three Member Sets,一个primary和两个secondary.我们先就搭建一个这样的测试环境. 首先建立三个数据目录和日志目录: cd ...