【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 ...
随机推荐
- 【OpenGL】初识OpenGL4.0
目录(?)[-] 什么是GLSL GLEW 安装GLEW 使用GLEW 其他库 使用GLM库进行数学运算 安装GLM 使用GLM 使用GLM作为OpenGL的输入 使用GLFW进行窗口管理 这篇文章主 ...
- 【DSP开发】回马枪要你命 德州仪器发布最强ARM芯片Keystone II
之前许多传闻称德州仪器将会彻底放弃OMAP系列ARM处理器,从此离开手持设备的江湖.如果你信以为真,那可就太小看德州仪器这个老狐狸了--要知道德州仪器诞生的比Intel都还早几年.三小时前,德州仪器宣 ...
- 第一次编译ffmpeg
今天开始玩ffmpeg了. 从官网下载来的压缩包,不会编译诶,于是我开始研究起来了. 下面就是实时记录的随笔: 首先是从官网下载来的ffmpeg,就是下面这个版本,目前的最新版吧. http://ff ...
- python面向对象的多态-类相关内置函数-类内置魔法函数-迭代器协议-上下文管理-04
多态 一种事物具备不同的形态 例如:水 --> 固态.液态.气态 多态:# 多个不同对象可以相应同一个对象,产生不同的结果 首先强调,多态不是一种特殊的语法,而是一种状态,特性(多个不同对象可以 ...
- 安装PIG
下载Pig 能够执行在Hadoop 0.20.* http://mirror.bit.edu.cn/apache/pig/pig-0.11.1/pig-0.11.1.tar.gz 也能够依据你的Had ...
- vim /etc/security/limits.conf中的hard和soft
转自:https://blog.csdn.net/zxljsbk/article/details/89153690 "soft" 和 "hard" 的区别sof ...
- Vue2.X 通过 ajax 获取 API 数据(非 axios)
不多废话,笔记如下 1. javascript: let vm = new Vue({ el: '#card-text', data: { info: '' }, beforeCreate: func ...
- 09 Scrapy框架以及基本使用
一.什么是scrapy? 是为了爬取网站数据,提取结构性数据而编写的应用框架.之所以叫做框架是因为集成了各种实用功能(高性能异步下载,队列,分布式,解析,持久化等等)的项目模板.对于框架的学习,重点是 ...
- Centos7:zookeeper安装,配置与使用
配置jdk环境 解压缩zookeeper的压缩包 配置 创建data目录 复制zoo_sample.cfg为zoo.cfg 修改confg/zoo.cfg中dataDir=**/data 常用命令 . ...
- el-table + el-form实现可编辑表格字段验证
表格输入信息很常见,因此表格的验证也很必要,el-form提供了输入框验证.可以和表格结合起来用,使用效果 HTML: <div class="table_box"& ...