LeetCode 318. Maximum Product of Word Lengths
来源:互联网 发布:js 对象访问器 编辑:程序博客网 时间:2024/06/11 03:32
题目链接:
https://leetcode.com/problems/maximum-product-of-word-lengths/description/
题解:
一道很有趣的题目,用常规的思路,时间应该是O(n^3),应该是会超时的(虽然自己没有试过。。。。。。),因此,我们就需要优化自己的算法,
我们可以对字符先进行预处理,这里我们可以对于每一个字符串进行状态的压缩,这样的话,我们在对其求长度的时候,只需要O(n^2)的时间。
代码:
class Solution {public: int maxProduct(vector<string>& words) { vector<int>num(words.size(),0); for(int i=0;i<words.size();i++) for(int j=0;j<words[i].size();j++) num[i]|=1<<(words[i][j]-'a'); int MAX=0; for(int i=0;i<words.size();i++) for(int j=i+1;j<words.size();j++) if(!(num[i]&num[j])) MAX=max((int)(words[i].size()*words[j].size()),MAX); 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
- LeetCode: 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
- [LeetCode]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
- leetcode 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
- 【leetcode】318. Maximum Product of Word Lengths
- [leetcode] 318. Maximum Product of Word Lengths
- leetcode 318. Maximum Product of Word Lengths
- C# 如何给Word文档设置背景颜色和背景图片
- 树状数组简单总结
- STM32 串口IAP
- EventBus3.0的使用
- 二进制乱搞——Luogu3917 异或序列
- LeetCode 318. Maximum Product of Word Lengths
- JS中apply()与call()的区别
- 刷题——Cow Bowling POJ
- Android 简单的播放器
- 嵌入式开发-迅为I.MX6Q开发板搭建系统基础编译环境
- C++右值引用的示例
- IOS基础--IOS9之允许Http请求
- SVM
- 初学oracle 第二天笔记 sqlplus