318. Maximum Product of Word Lengths
来源:互联网 发布:网络男女对唱歌曲大全 编辑:程序博客网 时间:2024/05/29 19:46
这道题给我们了一个单词数组,让我们求两个没有相同字母的单词的长度之积的最大值。我开始想的方法是每两个单词先比较,如果没有相同字母,则计算其长度之积,然后每次更新结果就能找到最大值。
我只想到了暴力的方法,所以看了大神的博客,发现确实是要一个一个比较,但是不是比较两个字符串,而是压缩成一个个int,按位相与,如果结果是0说明都不相同,则可以相乘。
class Solution {public: int maxProduct(vector<string>& words) { int res = 0; vector<int> mask(words.size(), 0); for(int i = 0; i < words.size(); ++i){ for(char c : words[i]){ mask[i] |= 1 << (c - 'a'); } for(int j = 0; j < i; ++j){ if(!(mask[i] & mask[j])){ res = max(res, int(words[i].size() * words[j].size())); } } } return res; }};
阅读全文
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
- button与验证控件的矛盾
- python中configParser模块的基础用法
- 隐藏的love
- 输入0~N的所有数
- 使用函数求奇数和
- 318. Maximum Product of Word Lengths
- clob字段升级到Oracle DB 12.2的问题
- 浏览器的兼容性问题
- C语言笔记(关键字)
- React 条件渲染 if
- tensorflow练习11:语音识别程序
- Java中的堆和栈
- 【java面试题】2017.11.17
- java -- while循环拆分性能测试程序