LeetCode 318. 最大单词长度乘积
作者:互联网
思路:快速判断2个单词是否有重复单词,用26位二进制数表示状态,如果&后是0则可以算乘积,暴力
代码:
class Solution {
public:
int maxProduct(vector<string>& words) {
vector<int> state;
for(auto word:words){
int s=0;
for(auto c:word)
s|=1<<(c-'a');
state.push_back(s);
}
int res=0;
for(int i=0;i<words.size();i++){
for(int j=i+1;j<words.size();j++)
if((state[i]&state[j])==0)
res=max(res,(int )(words[i].size()*words[j].size()));
}
return res;
}
};
标签:318,word,乘积,int,auto,单词,vector,words,LeetCode 来源: https://blog.csdn.net/qq_43619680/article/details/123649675