【leetcode】Maximum Product of Word Lengths
Given a string array words
, find the maximum value of length(word[i]) * length(word[j])
where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.
Example 1:
Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
Return 16
The two words can be "abcw", "xtfn"
.
Example 2:
Given ["a", "ab", "abc", "d", "cd", "bcd", "abcd"]
Return 4
The two words can be "ab", "cd"
.
Example 3:
Given ["a", "aa", "aaa", "aaaa"]
Return 0
No such pair of words.
此题如果用暴力比较法,也就是两层循环依次比较的话,执行会超时。
可以通过类似C++中bitset的方法来保存每个单词的一个key值,然后直接用key值进行比较,减少单词之间比较的时候比较字符的时间。
比如单词
abcw
,我们可以创建一个l = [0,0,0.....0] 有27个0的数组,并按26个字母的顺序依次给a-z索引为1~26,最后把a,b,c,w四位对应的元素的值置为1,计算 pow(2,1)+pow(2,2)+pow(2,3)+pow(2,23)的和即为这个元素的key值。
再用这个key值与其他元素的key值做与操作,结果为0,则表示单词无相同的字符。
下面是代码:
class Solution(object):
index = []
def transChar(self,c):
l = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
return l.index(c) + 1
def parseWords(self,w):
t = 0
l = []
for i in range(27):
l.append(0)
for i in set(w): #注:这里用set过滤掉相同的字符,我最初直接用w,导致运行超时
t = self.transChar(i)
l[t] = 1
t = 0
for i in range(len(l)):
if l[i] == 1:
t = t + pow(2,i)
#print w,t
return t
def maxProduct(self, words):
"""
:type words: List[str]
:rtype: int
""" max = 0
if len(words) == 0:
return 0
l = []
for i in words:
l.append(self.parseWords(i)) for i in range(len(l)):
for j in range(i+1,len(l)):
if l[i] & l[j] == 0:
if max < len(words[i]) * len(words[j]):
max = len(words[i]) * len(words[j])
return max
【leetcode】Maximum Product of Word Lengths的更多相关文章
- leetcode 318. Maximum Product of Word Lengths
传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...
- leetcode@ [318] Maximum Product of Word Lengths (Bit Manipulations)
https://leetcode.com/problems/maximum-product-of-word-lengths/ Given a string array words, find the ...
- Java [Leetcode 318]Maximum Product of Word Lengths
题目描述: Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where ...
- [leetcode]318. Maximum Product of Word Lengths单词长度最大乘积
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- 【Leetcode】Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- LeetCode 318. Maximum Product of Word Lengths (状态压缩)
题目大意:给出一些字符串,找出两个不同的字符串之间长度之积的最大值,但要求这两个字符串之间不能拥有相同的字符.(字符只考虑小写字母). 题目分析:字符最多只有26个,因此每个字符串可以用一个二进制数来 ...
- Leetcode 318 Maximum Product of Word Lengths 字符串处理+位运算
先介绍下本题的题意: 在一个字符串组成的数组words中,找出max{Length(words[i]) * Length(words[j]) },其中words[i]和words[j]中没有相同的字母 ...
- 【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 位运算 日期 题目地址:https://le ...
随机推荐
- 基于硬件的消息队列中间件 Solace 简介之二
前言...... 前面简单介绍了Solace来自于哪家公司, 主要能做哪些事情. 本篇主要进一步介绍Solace作为消息传递的中间件如何工作的. 传统意义上来讲, 每当我们谈到消息中间件时, 首先想到 ...
- 【监控实践】【4.4】使用DMV和函数监控数据库状态和资源使用
1.查看当前实例运行进程 -- 核心DMV.函数.系统SP:/* 所有进程请求:sys.dm_exec_requests 所有进程与连接:sys.sysprocesses 系统函数,查看sql:sys ...
- 对C++类的继承和派生的理解
C++中的继承是类与类之间的关系,是一个很简单很直观的概念,与现实世界中的继承类似,例如儿子继承父亲的财产. 1.继承(Inheritance)可以理解为一个类从另一个类获取成员变量和成员函数的过程. ...
- HTTPS原理(三次握手)
第一步: 客户端向服务器发送HTTPS请求,服务器将公钥以证书的形式发送到客户端(服务器端存放私钥和公钥). 第二步: 浏览器生成一串随机数,然后用公钥对随机数和hash签名进行加密,加密后发送给服务 ...
- linux连接Windows系统之项目连接
在桥接模式下 在linux内需要设置 防火墙关闭 在Windows中连接 linux的ip连接 ***项目 在linux中命令行输入setup-->防火墙配置-->空格-->确定-- ...
- unittest之一框架、suite
1.unittest是Python的标准库里的模块,所以在创建测试方法时,需直接导入unittest即可 2.unittest框架的六大模块: 测试用例TestCase 测试套件TestSuit:测试 ...
- mysql连接数据库时报2003错误怎么解决
mysql 2003是连接错误,连不上服务器. 你目前可以如下方法:进入控制面板->服务管理(我的是管理工具),->服务,然后找到Mysql服务,右键修改属性,改为自启动,以后再重启就没有 ...
- 14、PCA分析
做芯片PCA主成分分析可以选择使用affycoretools包的plotPCA方法,以样品"GSM363445_LNTT.CEL"."GSM362948_LTT.CEL& ...
- 在web项目中配置log4j
在web.xml中添加如下代码 <context-param> <param-name>contextConfigLocation</param-name> < ...
- O009、KVM 网络虚拟化基础
参考https://www.cnblogs.com/CloudMan6/p/5289590.html 网络虚拟化是虚拟化技术中最复杂的部分,学习难度最大. 但因为网络是虚拟化中非常重要的资源, ...