leetcode318. Maximum Product of Word Lengths
来源:互联网 发布:指南针炒股软件诈骗: 编辑:程序博客网 时间:2024/05/29 19:54
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.
Example 1:
Given [“abcw”, “baz”, “foo”, “bar”, “xtfn”, “abcdef”]
Return 16
The two words can be “abcw”, “xtfn”.
Example 2:
Given [“a”, “ab”, “abc”, “d”, “cd”, “bcd”, “abcd”]
Return 4
The two words can be “ab”, “cd”.
Example 3:
Given [“a”, “aa”, “aaa”, “aaaa”]
Return 0
No such pair of words.
用26个字母来标记
a->0
b->1
将单词转化为二进制
算法复杂度O(n^2)
class Solution(object): def maxProduct(self, words): """ :type words: List[str] :rtype: int """ ans=[] for i in words: tmp=0 for j in set(i): tmp=tmp+(1<<(ord(j)-ord('a'))) ans.append(tmp) MAX_P=0 for i in range(len(words)): for j in range(i,len(words)): if ans[i] & ans[j]==0: MAX_P=max(MAX_P,len(words[i])*len(words[j])) return int(MAX_P)
0 0
- leetcode318-Maximum Product of Word Lengths
- leetcode318. Maximum Product of Word Lengths
- leetcode318:Maximum Product of Word Lengths(medium)
- LeetCode318——Maximum Product of Word Lengths,从time limit exceeded到accept
- LeetCode318:Maximum Product Of Word Length
- Maximum Product of Word Lengths
- Maximum Product of Word Lengths
- Maximum Product of Word Lengths
- Maximum Product of Word Lengths
- Maximum Product of Word Lengths
- Maximum Product of Word Lengths
- Maximum Product of Word Lengths
- Maximum Product of Word Lengths
- Maximum Product of Word Lengths
- Maximum Product of Word Lengths
- Maximum Product of Word Lengths
- Maximum Product of Word Lengths
- Maximum Product of Word Lengths
- c#实现每隔一段时间执行代码(多线程)
- Java-类库-Guava-Multiset
- 【MAC】显示/隐藏Mac隐藏文件
- Ubuntu 14.04.4 LTS 设置静态IP (NET模式下)
- hrbust 1349 Graph【Floyd逆向思维】
- leetcode318. Maximum Product of Word Lengths
- The server time zone value '?????' more than one time zone
- Oracle以甲乙丙丁来排列
- web项目网站页面出现乱码的解决方法
- iOS GPUImage之头文件说明
- 【C++】MFC 创建对话框,实现对课程信息和学生信息的管理
- 在线支付平台的设计
- [知其然不知其所以然-30] smp wakeup nonboot CPUs
- Canvas对角线渐变