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 ...
随机推荐
- DevExpress gridControl 设置分组
有些代码非常有用,但是用的时候就记不清怎么写,所以就在这里打个草稿. //设置组汇总 private void SetSummation() { this.gridViewShipment.Group ...
- Guid和Sequence做主键的比较
记得A项目组是一个物流管理系统,后台采用了Oracle数据库.在系统中的核心表托运单表中,关于主键采用何种数据类型,是 sequence 还是用GUID , 大家起了争论. 从网络搜索得到的结论看,一 ...
- 图表控件FlowChart.NET详细介绍及免费下载地址
FlowChart.NET是一款专业的.NET平台下的流程图及图表控件,它可以运行在任何C#, VB.NET或Delphi.NET语言编写的软件中.能够帮助你创建工作流程图.对象层次和关系图.网络拓扑 ...
- windows下scrapy安装
C:\users\XXXX>easy_install scrapy 出现错误 fatal error C1083: Cannot open include file: 'openssl/aes. ...
- Xen启动过程分析(还是分享过来吧,找了好长时间)
XEN启动过程 Xen Hypervisor运行在Ring0,在启动过程中,Xen首先被引导:系统由Grub启动,遵循Multiboot引导规范:然后Linux内核做为module也被引导入 ...
- Java常用的输入输出方法
对于经常上机刷题的来说,首先得解决输入输出方法,Java的输入输出流在Java学习过程的后面部分才会接触,但是我们可以掌握一些简单的,常用的输入输出方法 首先输出 大家最熟悉的莫过于输出方法,直接用S ...
- 关于python函数的学习记录
1.默认参数必须指向不变对象! 2. extra = {'city': 'Beijing', 'job': 'Engineer'}注意kw获得的dict是extra的一份拷贝,对kw的改动不会影响到函 ...
- [转载]HDU 3478 判断奇环
题意:给定n个点,m条边的无向图(没有重边和子环).从给定点出发,每个时间走到相邻的点,可以走重复的边,相邻时间不能停留在同一点,判断是否存在某个时间停留在任意的n个点. 分析: (1)首先,和出发点 ...
- 安装go语言,配置环境及IDE,只需3步
安装go语言,配置环境及IDE,只需3步 ( 欢迎加入go语言群: 218160862 , 群内有实践) 第1.下载 go压缩包,解压 ,如果你是window系统,请选择go1.5.windows ...
- Python学习-day2
这周时间充裕,把第一周的两个作业登陆验证和三级菜单做完后又用零零散散的时间看完了第二周的课程,不得不说老男孩这个教育方式感觉还是不错的,其实说白了就是花钱找个人监督自己学习呗,而且还强行让我们养成一些 ...