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 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.
思路:对每一个元素都进行掩码设置,因为一共就26个字母,所以1个int的bit位就可以代表一个字母,比如"a"就可以表示成0000 0000 0000 0000 0000 0000 0000 0001,"bc"可以表示成0000 0000 0000 0000 0000 0000 0000 0110;如果两个元素的掩码位与以后结果为0,表示他们之间没有相同的字母,则乘积就是两串字符串的长度的乘积。
代码
class Solution {
public:
int maxProduct(vector<string>& words) {
vector<int> bitMask(words.size(), 0);
for( int i = 0; i < words.size(); i++ ){
for( int j = 0; j < words[i].size(); j++ ){
bitMask[i] |= 1<<(words[i][j]-'a');
}
}
int maxProduct = 0;
for( int i = 0; i < words.size(); i++ ){
for( int j = i + 1; j < words.size(); j++ ){
if( (bitMask[i]&bitMask[j] )== 0){
int len1 = words[i].size();
int len2 = words[j].size();
maxProduct = max(maxProduct, (len1*len2));
}
}
}
return maxProduct;
}
};
LeetCode 【318. 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】318. Maximum Product of Word Lengths 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 位运算 日期 题目地址:https://le ...
- 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 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]中没有相同的字母 ...
- 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 ...
- 318. Maximum Product of Word Lengths ——本质:英文单词中字符是否出现可以用26bit的整数表示
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
随机推荐
- 008-Scala主构造器、私有构造器、构造器重载实战详解
008-Scala主构造器.私有构造器.构造器重载实战详解 Scala主构造器实战 无参数的主构造器 分析 1.name 需要赋初值,一般通过占位符来代表空值 2.private 声明私有的age 生 ...
- java获取日期 昨天 今天 明天的日期
Date date=new Date();//取时间 Calendar calendar = new GregorianCalendar(); calendar.setTime(date); cale ...
- 【转】 Linux shell的&&和||
http://www.2cto.com/os/201302/189655.html Linux shell的&&和|| shell 在执行某个命令的时候,会返回一个返回值,该返回值 ...
- 记录Js
1.对于js,没有系统的学习.有要经常的用到,每次都是百度查找,为了以后能查询. (1). $(function () { $('.restbtn').on("click", fu ...
- 简述Git(Linux、Android~~开源)
Git——源代码管理软件,Android及Linux内核,驱动开发的过程中涉及的大量的源代码,都由Git管理 (一)安装Git Ubuntu Linux10.10或更新的版本,使用下面命令来安装Git ...
- JAVA双列集合HashMap
HashMap 双列集合HashMap是属于java集合框架3大类接口的Map类, Map接口储存一组成对的键-值对象,提供key(键)到value(值)的映射.Map中的key不要求有序,不允许 ...
- 怎么在excel中快速查找重复记录
假设数字在A列,数字从A1开始:方法一:辅助列中输入公式,数据重复时会出现“重复”提示.=IF(COUNTIF(A:A,A1)>1,"重复","") ,下 ...
- Unity使用protobuf-net进行二进制序列化与反序列化
Protobuf-net提供的一种易于使用的数据序列化方案,可序列化带有[ProtoContract]特性的类实例,并可支持Unity各个发布平台,且效率高.易用性强. public static c ...
- 纠结attr(),prop()
刚刚看博客无意中看到attr()和prop()的区别,回头就去翻了一下手册,感觉手册上写的过于简单,不能很清晰的分辨出两者的区别,两者的参数用法都是高度相似. attr():设置或返回被选元素的属性值 ...
- sql server 中xml 数据类型的insert、update、delete
近日对SQL操作XML作了如下整理: 1.插入 XML DECLARE @myDoc XMLSET @myDoc = '<Root> <ProductDescription Prod ...