[leetcode]Anagrams @ Python
原题地址:https://oj.leetcode.com/problems/anagrams/
题意:
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
解题思路:anagram的意思是:abc,bac,acb就是anagram。即同一段字符串的字母的不同排序。将这些都找出来。这里使用了哈希表,即Python中的dict。针对前面的例子来讲,映射为{abc:abc,bac,acb}。
代码:
class Solution:
# @param strs, a list of strings
# @return a list of strings
def anagrams(self, strs):
dict = {}
for word in strs:
sortedword = ''.join(sorted(word))
dict[sortedword] = [word] if sortedword not in dict else dict[sortedword] + [word]
res = []
for item in dict:
if len(dict[item]) >= 2:
res += dict[item]
return res
[leetcode]Anagrams @ Python的更多相关文章
- LeetCode专题-Python实现之第28题: Implement strStr()
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第27题:Remove Element
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第21题:Merge Two Sorted Lists
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第20题:Valid Parentheses
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第9题:Palindrome Number
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第13题:Roman to Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第7题:Reverse Integer
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
随机推荐
- Linux学习笔记02—磁盘分区
下面介绍四种最常见的分区方式: (1) 最简单的分区方案. SWAP分区:即交换分区,建议大小是物理内存的1-2倍. /分区:建议大小在6GB以上. 使用以上的分区方案,所有的数据都在/分区上, ...
- HTML5中的跨文档消息传递
跨文档消息传送(cross-document messaging),有时候也简称为XDM,指的是来自不同域的页面间传递消息.例如,www.w3cmm.com域中的一个页面与一个位于内嵌框架中的p2p. ...
- Using Dtrace OEL 6.X
http://www.hhutzler.de/blog/using-dtrace/ http://docs.oracle.com/cd/E37670_01/E38608/html/dt_sdtparg ...
- Linux驱动开发——指针和错误值
参考: <Linux设备驱动程序>第三版 P294 许多内部的内核函数返回一个指针值给调用者,而这些函数中很多可能会失败.在大部分情况下,失败是通过返回一个NULL指针值来表示的.这种技巧 ...
- 再谈vc发送键盘、组合键消息
关于向Windows窗口发送Alt组合键的问题,这个真是经典问题啊,在网上找了一下,问的人N多,方法差不多, 但就是没有很好解决问题. 之前找到一个能正确发送的code:(Alt+A) PostMes ...
- C# 连接Oracle数据库,免安装oracle客户端
一.方案1 首先下面的内容,有待我的进一步测试和证实.18.12.20 被证实了,还需要安装Oracle客户端,或者本机上安装oracle数据库软件. 18.12.20 1.下载Oracle.Mana ...
- C#编程(三十九)----------比较对象的相等性
比较对象的相等性 需要理解对象相等的机制对逻辑表达式的编程很重要,另外,对实现运算符重载和类型强制转换也很重要. 对象相等的机制有所不同,这取决于比较的是引用类型还是值类型. 比较引用类型的相等性 S ...
- 【elasticsearch】关于elasticSearch的基础概念了解【转载】
转载原文:https://www.cnblogs.com/chenmc/p/9516100.html 该作者本系列文章,写的很详尽 ================================== ...
- apk中添加第三方so文件
如果你是把so放在libs/armeabi/下,eclipse中so会自动打包进去,然后使用System.load("data/data/xxx.xxx.xxx/lib/xx.so" ...
- C语言之基本算法24—黄金切割法求方程近似根
//黄金切割法! /* ================================================================ 题目:用黄金切割法求解3*x*x*x-2*x* ...