文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

2. Solution

解析: Version 1每次迭代都需要进行两次set转换,Version 2预先进行了set转换,效率有提升。由于Version 2中两个字符串的与运算非常耗时,因此Version 3先将字符串转换为代表字符串的数字,然后进行与运算,这样两个字符串的与运算就变成了两个数字按位进行与运算,大大减少了运行时间。Version 4将代表数字一样的字符串进行了合并,并保留了最大的字符串长度,这样减少了与运算的迭代次数,并且不影响最终结果。

  • Version 1
class Solution:def maxProduct(self, words: List[str]) -> int:words.sort(key=len, reverse=True)maximum = 0n = len(words)for i in range(n):for j in range(i+1, n):x = len(words[i])y = len(words[j])if maximum >= x * y:return maximumintersection = set(words[i]) & set(words[j])if len(intersection) == 0:maximum = max(maximum, x * y)breakreturn maximum
  • Version 2
class Solution:def maxProduct(self, words: List[str]) -> int:chars = [set(word) for word in words]maximum = 0n = len(words)for i in range(n):for j in range(i+1, n):x = len(words[i])y = len(words[j])intersection = chars[i] & chars[j]if len(intersection) == 0:maximum = max(maximum, x * y)return maximum
  • Version 3
class Solution:def maxProduct(self, words: List[str]) -> int:lengths = [len(word) for word in words]flags = []for word in words:flag = 0for ch in word:flag = flag | (1 << (ord(ch) - 97))flags.append(flag)maximum = 0n = len(words)for i in range(n):for j in range(i+1, n):if flags[i] & flags[j] == 0:maximum = max(maximum, lengths[i] * lengths[j])return maximum
  • Version 4
class Solution:def maxProduct(self, words: List[str]) -> int:maximum = 0mapping = {}for word in words:flag = 0for ch in word:flag = flag | (1 << (ord(ch) - 97))mapping[flag] = max(mapping.get(flag, 0), len(word))for key1, value1 in mapping.items():for key2, value2 in mapping.items():if key1 & key2 == 0:maximum = max(maximum, value1 * value2)return maximum

Reference

  1. https://leetcode.com/problems/maximum-product-of-word-lengths/

Leetcode 318. Maximum Product of Word Lengths相关推荐

  1. leetcode 318. Maximum Product of Word Lengths | 318. 最大单词长度乘积

    题目 https://leetcode.com/problems/maximum-product-of-word-lengths/ 题解 中规中矩的题目,中规中矩的思路,不像是一个 medium 题. ...

  2. 318. Maximum Product of Word Lengths

    问题:给定一个字符串数组words,找到这样的最大值:length(word[i]) * length(word[j]),words[i]和words[j]没有共同的字母.假设输入字符串只包含小写字母 ...

  3. Maximum Product of Word Lengths

    Maximum Product of Word Lengths 题目链接: https://leetcode.com/problems... public class Solution {public ...

  4. LeetCode Maximum Product of Word Lengths(位操作)

    问题:给出一个字符串数组,要求求出两个没有共同字符的字符串的最大积 思路:第一种方法是直接枚举任意两个字符串,看是否有公共字符,如果没有,则计算乘积,并更新最大值. 第二种方法,因为字符范围是a-z, ...

  5. LeetCode 152. Maximum Product Subarray--动态规划--C++,Python解法

    题目地址:Maximum Product Subarray - LeetCode Given an integer array nums, find the contiguous subarray w ...

  6. LeetCode 628. Maximum Product of Three Numbers

    题目: Given an integer array, find three numbers whose product is maximum and output the maximum produ ...

  7. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  8. LeetCode 152. Maximum Product Subarray

    152. Maximum Product Subarray Find the contiguous subarray within an array (containing at least one ...

  9. Leetcode 2233. Maximum Product After K Increments

    题目 解法1 首先理解一下题意,要求的是对数组中某些数字增加1后的最大乘积,一共可增加k次.对于每一次增加1,整个乘积的变化是除了被增加这个数字以外的数字总和.那么最后乘积最大也就是k次增加的最多.从 ...

  10. leetcode 1339. Maximum Product of Splitted Binary Tree | 1339. 分裂二叉树的最大乘积(树形dp)

    题目 https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/ 题解 树形 dp,思路见草稿 /*** Defini ...

最新文章

  1. TCP超时与重传机制与拥塞避免
  2. ThinkPHP函数详解:M方法
  3. 易语言html规则分析,易语言算法原理浅析【一】(示例代码)
  4. hadoop2.7 伪分布
  5. Sentinel的简单使用
  6. ModuleNotFoundError: No module named 'cv2' (安装cv2)
  7. 理清contactsprovider
  8. FPGA入门例程:LED
  9. 西电软件工程概论复习纲要
  10. ORACLE11G学习视频
  11. html5 live,html5 audio livestreaming
  12. 虫虫吃第一颗豆子---第一次作业
  13. linux nmblookup 获取不到数据,接口中可以查到数据,为什么却获取不到呢?
  14. 对你来说,哪一个深度学习网络是最佳选择?(2)
  15. Java项目:ssm+jsp实现手机WAP版外卖订餐系统
  16. java鸡兔同笼用循环_Java使用for循环解决经典的鸡兔同笼问题示例
  17. 深度学习(七)——图像验证码破解(数字加减验证码)
  18. CSS3实现无限循环的无缝滚动
  19. 接口测试小白的Testng学习之路--在eclips中安装Testng
  20. navicat12破解版

热门文章

  1. 高等数学Mathematica实验题——2.2 - 16. 欧拉常数的计算(Calculation of EulerGamma Constant)
  2. 高效率完成一次接入80个手游渠道SDK——游戏接入聚合SDK服务端篇
  3. 泛微oa ecology8.0创建工作流程示例
  4. 基于社会工程学的网络攻击手段分析
  5. 开源语音助手_开源语音助手软件入门
  6. java嵌套条件运算符_java条件运算符的嵌套使用
  7. 万字攻略,详解腾讯面试
  8. 《寻找下一个独角兽》天使投资8字箴言:看势、识人、论术、实战
  9. 用 线性回归 预测股票的涨跌
  10. 用SVM预测股票涨跌 - 免费分享全套代码