318. Maximum Product of Word Lengths
来源:互联网 发布:seo怎么提高自然排名 编辑:程序博客网 时间:2024/05/29 18:21
根据题目的描述,我们可知,最耗时的地方在于判断两个字符串中是否存在重复的字符。
一提到判断重复字符,脑海中马上浮现哈希大法。
又看到题目要保证字符串均为小写字母,也就说总共26个字母,我们需要26位,一个int足矣。
我们自定义,如果某字符出现,该bit置1,否则置0.
则比较两个字符串是否有重复字符,就是让这两个标志int 进行与操作,结果为0则表明没有重复字符。
好的,本题的难点已结束。code如下:
易知时间复杂度为O(n2),空间复杂度为O(n),其中n为所有字符串的个数。
class Solution {
public:int maxProduct(vector<string>& words) {
int n=words.size(),Max=0;
if(n<2) return 0;
int num[n]={0};
for(int i = 0; i < n;i++){
for(int j = 0;j<words[i].length();j++)
num[i] |= 1 << (words[i][j]-'a');
}
for(int i = 0; i < n-1; i++){
for(int j = i+1;j < n;j++){
if((num[i] & num[j]) == 0){
int m=words[i].length()*words[j].length();
if(m > Max) Max = m;
}
}
}
return Max;
}
};
0 0
- 【leetcode】318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths LeetCode
- [leetcode] 318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- LeetCode: 318. Maximum Product of Word Lengths
- LeetCode 318. Maximum Product of Word Lengths
- leetcode 318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- [LeetCode]318. Maximum Product of Word Lengths
- [LeetCode]318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- LeetCode 318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- LeetCode *** 318. Maximum Product of Word Lengths
- 318. Maximum Product of Word Lengths
- 【QTP】FSO读取/写txt文本乱码解决方法
- 数据库之mybook=mrcbook——书签的应用
- Node.js背后的V8引擎优化技术
- 关于图像处理
- el常用的字符串截取
- 318. Maximum Product of Word Lengths
- Unity3D笔记 地形、光源、天空盒子
- ATL的特点与基本使用方法
- 深拷贝和浅拷贝和copy和mutableCopy总结
- Hibernate注解映射的使用
- 跟我一起写 Makefile
- 工作邮件的若干注意事项
- 小白进阶之矩阵乘法
- 溶解与重现