Maximum Product of Word Lengths
来源:互联网 发布:ibm外包怎么样 知乎 编辑:程序博客网 时间:2024/05/16 15:52
leetcode第318题,要求在一串字符串中找出没有重复字母的单词,并且其长度之积最大。
tag是位操作,一开始没明白什么意思,后来参考后发现,单词全部由小写字母组成,因此只有26种可能,因此可以设置一个26位的数字来记录该位上字母是否出现,两个单词如果有相同的字母,那么这两个单词对应的代表的数字当按位与操作之后一定不是零。这样就可以就可以判断是否有重复字母了,然后遍历找出最大长度积就可以了,复杂度为o(n2)
class Solution(object): def maxProduct(self, words): """ :type words: List[str] :rtype: int """ nums = [] n = len(words) maxlen = 0 for word in words: nums.append(sum(1<<(ord(char)-ord('a')) for char in set(word))) for i in range(n): for j in range(n): if not (nums[i] & nums[j]): maxlen = max(len(words[i])*len(words[j]),maxlen) return maxlen
0 0
- 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
- Maximum Product of Word Lengths
- leetcode Maximum Product of Word Lengths
- Leetcode: Maximum Product of Word Lengths
- 【leetcode】318. Maximum Product of Word Lengths
- Leetcode202: Maximum Product of Word Lengths
- Redux和React
- 【55种开源数据可视化工具简介】
- select an archetype 空白--eclipse新建maven项目的bug
- 全面理解Unity加载和内存管理
- 【项目2 - 程序的多文件组织】
- Maximum Product of Word Lengths
- C# 读写TXT的两种方法
- ubuntu下打开Matlab报错:matlab is selecting software opengl rendering
- Android测试系列之一 - 测试分类(节选)
- Linux 访问windows共享
- (OK) Android - MPTCP - (net/ipv4/Kconfig) (ztg alter)
- 环信(Android)设置头像和昵称的方法(最简单暴力的基于环信demo的集成)。
- testCompile 'junit:junit:4.12'
- javascript同源策略