LeetCode—318 Maximum Product of Word Lengths
来源:互联网 发布:周璇 五月的风 知乎 编辑:程序博客网 时间:2024/06/05 05:41
思路:处理一下这些strs,排除掉重复的,建立二重map,第一个以字母作为key,对应的value是另外一个map,这个map的key是该单词的序列号。
Map<字符,Map<字符串的序列,标识符>>
GitHub地址:https://github.com/corpsepiges/leetcode
有Java版本和Python版本,求star。
Map<Character,Map<Integer,Integer>> map=new HashMap<Character, Map<Integer,Integer>>();//装初始字符串数组String[] initStrs=null;//装处理过的字符串数组String[] nowStrs=null; public int maxProduct(String[] words) { int length=words.length; if (length<2) {return 0;} initStrs=words; nowStrs=new String[length]; Map<String,Integer> cond=null; StringBuffer sb=null; //对原数组进行处理,保证处理后的字符串中没有重复字母,缩减判断时间 for (int i = 0; i < length; i++) { cond=new HashMap<String, Integer>(); sb=new StringBuffer(); for (int j = 0; j < words[i].length(); j++) { String k=words[i].substring(j,j+1);if (cond.get(k)==null) {sb.append(k);cond.put(k,1);}} nowStrs[i]=sb.toString();} //遍历原有数组,按字符写进map for (int i = 0; i < length; i++) {char[] cs=nowStrs[i].toCharArray();for (int j = 0; j < cs.length; j++) {if (map.get(cs[j])==null) {Map<Integer,Integer> m=new HashMap<Integer, Integer>();m.put(i, 1);map.put(cs[j], m);}else {map.get(cs[j]).put(i, 1);}}} int max=0; for (int i = 0; i < length; i++) {for (int j = i+1; j < length; j++) {int test=f(i,j);if (test>max) {max=test;}}} return max; } public int f(int i,int j){ char[] csi=nowStrs[i].toCharArray(); //选择一个字符串开始读取,如果该字符串包含的任意一个另外字符串拥有的字符,则返回0 for (int k = 0; k < csi.length; k++) {if (map.get(csi[k]).get(j)!=null) {return 0;}} return initStrs[i].length()*initStrs[j].length(); }
0 0
- [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 Maximum Product of Word Lengths
- Leetcode: Maximum Product of Word Lengths
- 【leetcode】318. Maximum Product of Word Lengths
- leetcode Maximum Product of Word Lengths
- leetcode -- Maximum Product of Word Lengths -- 重点
- Maximum Product of Word Lengths | LeetCode
- Leetcode: Maximum Product of Word Lengths
- LeetCode:Maximum Product of Word Lengths
- 使用JAVA Robot 扩展Webdriver 模拟键盘鼠标操作
- 阿里云 Ubuntu 14.04 安装mysql 5.6
- [iOS]关于__unsafe_unretained与__weak
- 用友ERP-供应链1-模块及价值
- spark提交应用的全流程分析
- LeetCode—318 Maximum Product of Word Lengths
- SSM回滚测试(给自己看)
- linux下如何手动更新Firefox
- OC语言 常用数据类型=>>NSData & NSMutableData
- 如何 解包 ,编辑 ,重新打包boot images
- CSocketClient.cpp
- 分布式哈希表DHT和一致性哈希
- 浅复制&深复制
- jquery实现简单的图片查看器