<LeetCode OJ> 318. Maximum Product of Word Lengths
来源:互联网 发布:java如何比较日期大小 编辑:程序博客网 时间:2024/05/29 09:11
题目翻译:
给定一个字符串数组 words找到最大值 length(word[i]) * length(word[j])且这两个词必须不包含共同的字符。你应该认为每个单词只包含小写字母。
如果不存在这样的两个词,返回0。
示例1:
给定 ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
返回 16
这两个词 "abcw", "xtfn".
示例2:
给定 ["a", "ab", "abc", "d", "cd", "bcd", "abcd"]
返回 4
这两个词 "ab", "cd".
示例3:
给定 ["a", "aa", "aaa", "aaaa"]
返回 0
没有这样的字。
分析:DONE
总是查找两个字符串是否有相同的字符(耗时耗力),没有获取两者的长度乘积,有就无视两者。
笨办法处理(超时):
class Solution {public: int maxProduct(vector<string>& words) { if(words.empty()) return 0; int result=0; for(int i=0;i<words.size()-1;i++) { for(int j=i+1;j<words.size();j++) { if(!isCommom(words[i],words[j]) && words[i].size()*words[j].size() > result) result=words[i].size()*words[j].size(); } } return result; } bool isCommom(string stra,string strb)//判断是否有共同元素 { if(stra.size()==0 || strb.size()==0) return false; sort(stra.begin(),stra.end()); sort(strb.begin(),strb.end()); int i=0; int j=0; if(stra[stra.size()-1] < strb[0] || stra[0] > strb[strb.size()-1]) return false; while(i<stra.size() && j<strb.size()) { if(stra[i] < strb[j]) i++; else if(stra[i] > strb[j]) j++; else return true; } return false; }};
别人的分析:
漂亮,这种位处理方法对于字符的处理似乎很常用,在前面某个题目见过这种做法!
根据题目的描述,我们可知,最耗时的地方在于判断两个字符串中是否存在重复的字符。
一提到判断重复字符,脑海中马上浮现哈希大法。
又看到题目要保证字符串均为小写字母,也就说总共26个字母,我们需要26位,一个int足矣。
我们自定义,如果某字符出现,该bit置1,否则置0.
则比较两个字符串是否有重复字符,就是让这两个标志int 进行与操作,结果为0则表明没有重复字符。
好的,本题的难点已结束。code如下:
易知时间复杂度为O(n2),空间复杂度为O(n),其中n为所有字符串的个数。
class Solution {public: int maxProduct(vector<string>& words) { if(words.empty()) return 0; vector<int> isDuplicate(words.size(),0); int nSize = words.size(); //处理的很漂亮 for(int i = 0; i < nSize; i++) { //将第i个字符串转化成在1个int里,比如abc,将被转化成0000...0111 int length = words[i].size(); for(int j = 0; j < length; j++) isDuplicate[i] |= 1 << (words[i][j] - 'a'); //将1移动到字符相应位置,比如c就应该在000..0100中的1位置 } int maxMul = 0; for(int i = 0; i < nSize -1; i++) { for(int j = i +1; j < nSize; j++) { if((isDuplicate[i] & isDuplicate[j]) == 0)//相与结果为0,则说明无重复字符 { int tempMul = words[i].size()*words[j].size(); if(maxMul < tempMul) maxMul = tempMul; } } } return maxMul; }};
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/51660360
原作者博客:http://blog.csdn.net/ebowtang
本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895
- <LeetCode OJ> 318. Maximum Product of Word Lengths
- 【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
- 古代猪文 [Bzoj 1951,Sdoi 2010]
- 用递归的方法求解3
- June 11th 模拟赛C T1 Sandcas Solution
- CSP考试 2016年04月第2题 俄罗斯方块 C语言实现
- hdu 1863 畅通工程(最小生成树,kruskal)
- <LeetCode OJ> 318. Maximum Product of Word Lengths
- 设计模式系列(三)工厂模式
- html标签复习--2016.6.13
- MongoDB(三)主从复制以及副本集的配置和使用
- 集合框架基础和例题
- JAVA的MD5和SHA-256和SHA-512 的 Hash 算法的调用
- svn查看用户,切换用户
- 我本一无所有——读《明朝那些事儿》
- 【Nginx学习】url地址补全最后的/(反斜杠)