49-Group Anagrams-(Medium) 题解
1、题目
Given an array of strings, group anagrams together. For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"],
Return: [
["ate", "eat","tea"],
["nat","tan"],
["bat"]
]
Note: All inputs will be in lower-case.
2、分析,该题目可以针对每个字符串进行排序,然后计算每个字符串的hash值进行归类
3、优化、在计算hash值的时候,考虑hash算法跟字符串排序无关,也就是不用进行排序,针对26个字母分配26个质数,然后将hash函数设置为每个字母映射的质数乘积,便不用进行排序了,该方法利用了质数的特性
两个质数相乘得到一个合数,这个合数不会分解为其它质数的乘积
4、代码:
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
__author__ = 'qqvipfunction' primeTable = [2,3,5,7,11, 13,17,19,23,29, 31,37,41,43,47, 53,59,61,67,71, 73,79,83,89,97, 101] class Solution(object): def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
map = {}
for i in range(0 , len(strs)):
str = strs[i]
hash = self.hash_str(str)
list = map.get(hash, None)
if not list:
list = []
list.append(str)
map[hash] = list return map.values() def hash_str(self, str):
length = len(str)
charAvalue = ord('a')
if length > 0:
hashSum = 1
for i in range(0, length):
hashSum = hashSum * primeTable[(ord(str[i]) - charAvalue)]
return hashSum
return 0 if __name__ == '__main__':
s = Solution()
print(s.groupAnagrams(["eat", "tea", "bat"]))
49-Group Anagrams-(Medium) 题解的更多相关文章
- 刷题49. Group Anagrams
一.题目说明 题目是49. Group Anagrams,给定一列字符串,求同源词(包含相同字母的此)的集合.题目难度是Medium. 二.我的做法 题目简单,就不多说,直接上代码: class So ...
- LeetCode - 49. Group Anagrams
49. Group Anagrams Problem's Link ------------------------------------------------------------------ ...
- 49. Group Anagrams - LeetCode
Question 49. Group Anagrams Solution 思路:维护一个map,key是输入数组中的字符串(根据字符排好序) Java实现: public List<List&l ...
- 49. Group Anagrams
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- leetcode@ [49] Group Anagrams (Hashtable)
https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...
- 【LeetCode】49. Group Anagrams
题目: Given an array of strings, group anagrams together. For example, given: ["eat", " ...
- 【一天一道LeetCode】#49. Group Anagrams
一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: [" ...
- [leetcode]49. 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 ...
- 49. Group Anagrams (string, HashTable)
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
随机推荐
- Eclipse发布Maven项目到远程服务器
pom.xml中的配置依赖有两种: 一.tomcat-maven.plugin <plugin> <groupId>org.codehaus.mojo</groupId& ...
- 在新浪sae上部署WeRoBot
花了整整一个下午,终于在新浪sae部署完成WeRoBot,现在将其中的曲折记录下来. 首先下载WeRoBot-SAE-demo,按照README.md中的要求,执行下述命令: git clone gi ...
- 纯css三角形
三角形原理: 盒子c内容width为0,height为0,盒子给一定宽度的border,分别为四边的border设置不同的颜色,则可以得到不同样式的三角形举个简单的栗子:CSS代码 .box1{ wi ...
- inheritprototype原型继承封装及综合继承最简实例
1.inheritprototype.js ;(function(){ var s = { inheritObject:function(o){//对象继承封装 ...
- 兼容IE6及以上的导航栏子菜单栏滑过显示隐藏效果
;(function(window){ var li = document.getElementById('parentnav').getElementsByTagName('li')[ ...
- --@angularJS--angular与BootStrap3的应用
angularJS与BootStrap3是最佳搭档. 1.Form.html: <!doctype html><html ng-app="UserInfoModule&qu ...
- git 配置SSH免密
1.安装TortoiseGit(比较简单,直接在官网上下载安装包安装) 2.打开下图标识 点击 generate按钮 生成key(需要等一会) 3.生成Key保存成.ppk文件,记得存放路径,(建 ...
- Linux释放内存脚本
vi recover_mem.sh #!/bin/bash ################## #author:rrhelei@126.com# ################## #系统分配的 ...
- 交叉验证(cross validation)
转自:http://www.vanjor.org/blog/2010/10/cross-validation/ 交叉验证(Cross-Validation): 有时亦称循环估计, 是一种统计学上将数据 ...
- 关于异常“The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine”的处理
我们在利用SqlBlukcopy技术倒2010 或者2007格式的文件到SqlServer 数据库的时候,会发生如下错误: 原因如下: 1.在用SQL SERVER 2005访问.xlsx文件(off ...