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. Repeater嵌套绑定Repeater以及内层调用外层数据

    aspx: <table border=" style="margin-bottom: 5px" width="100%"> <as ...

  2. Windows正向绑定shell和反向反弹shell的Python代码

    Windows下的shell原理 经过查阅资料,使用os.dup2(nfd, ofd)的方式重定向socket的输入输出到windows系统的cmd是无法做到的,属于系统原因,不能直接复制Linux下 ...

  3. windows桌面通知区域不显示音量图标的解决方法

    在windows系统桌面通知区域一般都是显示的输入法.电源.网络.音量及托盘的应用程序等内容 通知区域图标显示与否与控制面板的通知区域图标的设置有关,我们有时会遇到通知区域不显示个别图标的情况,如不显 ...

  4. 在python pydev中使用todo标注任务

    在做自动化测试时,有部分代码因需求未定或界面需要更改,代码不做修改或更新,这里就需要用到TODO功能. 在PyCharm中TODO功能很详细,但在pydev中怎么用呢.看了文档后,截图如下: 1.设置 ...

  5. 远程服务器git搭建

    在远程服务器如:/var/www下创建hello.git 然后git init --bare hello.git cd hello.git会看到下面的目录和文件 然后创建可以访问git的用户 git ...

  6. 阿里云liunx-ubuntu安装中文

    ubuntu安装中文支持--让你的ub真正变成中文作者: 来源:ubuntu在安装的时候会提示选择语言,但选择了中文之后,系统里面还有很多英文并不是ub对中文支持不好 你根本就没有安装中文支持~ 首先 ...

  7. Scala学习笔记(2)-类型注意

    Scala类型注意事项: 1.Any是绝对的根,所有的其他可实例化类型均有AnyVal和AnyRef派生. 2.所有AnyVal的类型成为值类型(所有数值类型.char.Booble和Unit) 3. ...

  8. (sklearn)机器学习模型的保存与加载

    需求: 一直写的代码都是从加载数据,模型训练,模型预测,模型评估走出来的,但是实际业务线上咱们肯定不能每次都来训练模型,而是应该将训练好的模型保存下来 ,如果有新数据直接套用模型就行了吧?现在问题就是 ...

  9. Java基础类编程集锦

    1.计算1+2+3+4+5+6+7+8+9的值 package com.neusoft.chapter1; /** * @author zhao-chj *题:计算1+2+3+4+5+6+7+8+9的 ...

  10. Java编程思想第四版随书源码官方下载方法

    见不少人在找net.mindview.util.Print,CSDN上有下载,收积分,以下是官网的下载方法,免费: 官网链接:http://mindview.net/ 电子书下载地址:http://w ...