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 ...
随机推荐
- fastcgi是什么?与php-fpm之间是什么关系?
首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index.h ...
- 关于JavaScript和html的随笔
最近听了一些关于JavaScript和html的讲课和读了一些书籍.因为我是给项目做网站知道的,所以要特别的注意和努力.JavaScript是一门挺好用的脚本语言,比较简单灵活,在这上面我深有体会,因 ...
- 利用python实现爬虫爬取某招聘网站,北京地区岗位名称包含某关键字的所有岗位平均月薪
#通过输入的关键字,爬取北京地区某岗位的平均月薪 # -*- coding: utf-8 -*- import re import requests import time import lxml.h ...
- 动态生成tr,并将其下控件的值拼接后传到后台并保存
有两个表(主表和子表),现在需要根据主表某一个字段动态的生成记录(一条记录就一个tr),然后再讲tr下控件的各个值取出来,传到后台,并保存到子表. html代码: <!--#for(Record ...
- git stash -u 添加新文件
git 提交 有新文件执行 git stash -u ------ 如果已经执行git stash,会发现有UNtracked这个单词 说明新文件没有添加进去,此时 执行 git stash ...
- Apriori——python3实现
最近看了关联算法中的Apriori没看懂,这次看了一些论文总算看懂了,不过还是没能够自己实现.在github搜到一些代码看,看的不很懂,这里先贴上(当中有自己加的注释),有时间再补充研究. # -*- ...
- PHP实现文本快速查找 - 二分查找
PHP实现文本快速查找 - 二分查找法 起因 先说说事情的起因,最近在分析数据时经常遇到一种场景,代码需要频繁的读某一张数据库的表,比如根据地区ID获取地区名称.根据网站分类ID获取分类名称.根据关键 ...
- xml 读取递归算法
xml 读取递归算法:
- (利用tempdata判断action是直接被访问还是重定向访问)防止微信活动中用户绕过关注公众号的环节
说明:这个不是在进行微信公众号开发,也就是说在不能获取用户openid的前提下做的下面操作 1.动机:最近有个微信活动(关注了服务号的可以免费领取礼品),要做这么一个功能,活动的入口在微信服务号的菜单 ...
- jquery操作input值总结
获取选中的值获取一组radio被选中项的值var item = $('input[@name=items][@checked]').val(); 获取select被选中项的文本var item = $ ...