318. Maximum Product of Word Lengths
2016-03-14 13:17
288 查看
//超时算法 class Solution { public: int maxProduct(vector<string>& words) { // int num=words.size(); if(!words.size()) return 0; int product=0; bool flag1[26]; bool flag; for(auto a=words.begin();a!=words.end();a++) { memset(flag1,0,26); int len1=(*a).size(); for(int i=0;i<len1;i++) { if(!flag1[(*a)[i]-'a']) flag1[(*a)[i]-'a']=true; } for(auto b=a+1;b!=words.end();b++) { int len2=(*b).size(); flag=false; for(int i=0;i<len2;i++) { if(flag1[(*b)[i]-'a']) break; flag=true; } if(flag&&len2*len1>product) product=len2*len1; } } } }; //96ms class Solution { public: int maxProduct(vector<string>& words) { if(!words.size()) return 0; int len=words.size(); int *flag=new int[len]; int *len_word=new int[len]; int product=0; for(int j=0;j<len;j++ ) { flag[j]=0; len_word[j]=words[j].size(); for(int i=0;i<len_word[j];i++) { flag[j] |= 1<<(words[j][i]-'a'); } } for(int i=0;i<len;i++) { for(int j=i+1;j<len;j++) { if(!(flag[i]&flag[j]) && len_word[i]*len_word[j]>product) product=len_word[i]*len_word[j]; } } delete [] flag; delete [] len_word; return product; } };
相关文章推荐
- 299. Bulls and Cows
- ajax实现跨域的三种方法
- 2016一号特急文:三网融合的全国、全面、深入,IPTV再迎政策利好(来源:@张维才 中国网络视频产业论坛)
- AppAssure备份Windows平台Oracle数据库
- 【android】Android Studio 的鼠标悬浮提示
- 290. Word Pattern
- 283. Move Zeroes
- 263. Ugly Number
- 260. Single Number III
- 基于bootstrap的网页开发
- 深入理解JVM内幕
- (水)POJ-1979 Red and Black(dfs,bfs)
- ios 官方sample
- 258. Add Digits
- 242. Valid Anagram
- 238. Product of Array Except Self
- 237. Delete Node in a Linked List
- 235. Lowest Common Ancestor of a Binary Search Tree
- java实现smtp邮件功能
- .NET跨平台之旅:探秘 dotnet run 如何运行 .NET Core 应用程序