318. Maximum Product of Word Lengths
来源:互联网 发布:亿用户级软件 编辑:程序博客网 时间:2024/05/16 14:45
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.
s思路:
1. 只有小写字母,说明啥?说明只有26种可能,比int的32路并行数据还小,也就是说,把每个数含有每个字母的情况用一个int就给完全表示了,然后就两两相与,如果相与等于0,说明没有共同的字母咯。
- 26和32这个梗,确实要能接住,说白了就是看能不能把string的比较问题和int的操作联系在一起。想起之前说过的话,事物间都天然存在各种联系,就看自己有没有火眼金睛能一眼看破看穿。这也是本事,得练!但归根到底,是从心里接受这个观念,并长期使用这个观念!
- 另一个可以说的地方是,再次把一个int看成一个容器,可以装进去32种并行的结果,所以int就不只是一个普通的数了,这就是看问题的角度了,怎么看能看到不同的用途!
class Solution {public: int maxProduct(vector<string>& words) { // if(words.size()<=1) return 0; vector<int> res(words.size()); for(int i=0;i<words.size();i++){ for(char c:words[i]){ res[i]=res[i]|(1<<(c-'a')); } } int mx=0; for(int i=0;i<words.size()-1;i++){ for(int j=i+1;j<words.size();j++){ if((res[i]&res[j])==0){ mx=max(mx,int(words[i].size()*words[j].size())); //bug:size()的返回类型不是int,需要强制转换 } } } return mx; }};
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
- EL表达式 (详解)
- LCD的静态显示
- tensorflow reading data (1)
- 【转】如果看了此文你还不懂傅里叶变换,那就过来掐死我吧【完整版】
- 四轴自适应控制算法的一些尝试开源我的山猫飞控和梯度在线辨识自适应等算法—(转)
- 318. Maximum Product of Word Lengths
- Echarts学习之使用要点步骤
- 逻辑思维训练(六)
- [python3教程]第八章.ERRORS AND EXCEPTIONS
- final,finally,finalize的区别
- 闲聊几种学习方式,选择比努力更重要
- POJ2392-Space Elevator-多重背包
- iOS - 打包提审,常见的被拒原因及解决
- c# winform 窗口 拆分多个源代码文件