当前位置: 首页 > news >正文

微信网站开发系统网络推广团队

微信网站开发系统,网络推广团队,外贸箱包网站模板,wordpress优化打开速度插件目录 前言 方法一 方法二 前言 题目链接:318. 最大单词长度乘积 - 力扣(LeetCode) 题目: 输入一个字符串数组 words,请计算不包含相同字符的两个字符串 words[i] 和 words[j] 的长度乘积的最大值。如果所有字符串…

目录

前言

方法一

方法二


 


前言

题目链接:318. 最大单词长度乘积 - 力扣(LeetCode)

题目

输入一个字符串数组 words,请计算不包含相同字符的两个字符串 words[i] 和 words[j] 的长度乘积的最大值。如果所有字符串都包含至少一个相同的字符,那么返回 0。假设字符串中只包含英文小写字母。例如,输入的字符串数组 words 为 ["abcw", "foo", "bar", "fxyz", "abcdef"],数组中的字符串 "foo" 与 "bar" 没有相同的字符,它们的长度的乘积为 9;"abcw" 和 "fxyz" 也没有相同的字符,它们长度的乘积为 16,这是该数组中不包含相同字符的一对字符串的长度乘积的最大值。

分析

解决这个问题的关键在于如何判断两个字符串 str1 和 str2 中有没有相同的字符。一个直观的想法是基于字符串 str1 中的每个字符 ch,扫描字符串 str2 判断字符串 ch 是否出现在 str2 中。如果两个字符串的长度分别为 p 和 q,那么这种蛮力法的时间复杂度是 O(pq)。


方法一

可以用哈希表来优化时间效率。对于每个字符串,可以用一个哈希表记录出现在该字符串中的所有字符。在判断两个字符串是否有相同的字符时,只需要从 'a' 到 'z' 判断某个字符是否在两个字符串对应的哈希表中都出现了。在哈希表中查找的时间复杂度是 O(1)。这个题目假设所有字符都是英文小写字母,只有 26 个可能的字符,因此最多只需要在每个字符串对应的哈希表中查询 26 次就能判断两个字符串是否包含相同的字符。26 是一个常数,因此可以认为应用哈希表判断两个字符是否具有相同的字符的时间复杂度是 O(1)。

由于这个题目只需要考虑 26 个英文小写字母,因此可以用一个长度为 26 的布尔型数组来模拟哈希表。数组下标为 0 的值表示字符 'a' 是否出现,下标为 1 的值表示字符 'b' 是否出现,其余依次类推

写法一

class Solution {
public:int maxProduct(vector<string>& words) {int length = words.size();bool** flags = new bool*[length];for (int i = 0; i < length; ++i){flags[i] = new bool[26];for (int j = 0; j < 26; ++j){flags[i][j] = false;}}
​for (int i = 0; i < length; ++i){for (char ch : words[i]){flags[i][ch - 'a'] = true;}}
​int result = 0;for (int i = 0; i < length; ++i){for (int j = i + 1; j < length; ++j){int k = 0;for (; k < 26; ++k){if (flags[i][k] && flags[j][k])break;}
​if (k == 26){int product = words[i].size() * words[j].size();if (product > result)result = product;}}}
​for (int i = 0; i < length; ++i){delete[] flags[i];}delete[] flags;
​return result;}
};

写法二

class Solution {
public:int maxProduct(vector<string>& words) {int length = words.size();vector<vector<bool>> flags(length, vector<bool>(26, false));for (int i = 0; i < length; ++i){for (char ch : words[i]){flags[i][ch - 'a'] = true;}}
​int result = 0;for (int i = 0; i < length; ++i){for (int j = i + 1; j < length; ++j){int k = 0;for (; k < 26; ++k){if (flags[i][k] && flags[j][k])break;}
​if (k == 26){int product = words[i].size() * words[j].size();if (product > result)result = product;}}}
​return result;}
};


方法二

方法一是用一个长度为 26 的布尔型数组记录字符串中出现的字符。布尔值只有两种可能,即 true 或 false。这与二进制有些类似,在二进制中数字的每个数位要么是 0 要么是 1。因此,可以将长度为 26 的布尔型数组用 26 个二进制的数位代替,二进制的 0 对应布尔值 false,而 1 对应 true

C++ 中 int 型整数的二进制形式有 32 位,但只需要 26 位就能表示一个字符串中出现的字符,因此可以用一个 int 型整数记录某个字符串中出现的字符。如果字符串中包含 'a',那么整数最右边的数位为 1;如果字符串中包含 'b',那么整数的倒数第 2 位为 1,其余依次类推。这样做的好处是能更快地判断两个字符串是否包含相同的字符。如果两个字符串中包含相同的字符,那么它们对应的整数相同的某个数位都为 1,两个整数的与运算(&)将不会为 0;如果两个字符串没有相同的字符,那么它们对应的整数的与运算的结果等于 0

class Solution {
public:int maxProduct(vector<string>& words) {int length = words.size();vector<int> flags(length, 0);for (int i = 0; i < length; ++i){for (char ch : words[i]){flags[i] |= 1 << (ch - 'a');}}
​int result = 0;for (int i = 0; i < length; ++i){for (int j = i + 1; j < length; ++j){if ((flags[i] & flags[j]) == 0){int product = words[i].size() * words[j].size();if (product > result)result = product;}}}return result;}
};

注意:方法一和方法二的时间复杂度是同一个量级的,但是方法一在判断两个字符串是否包含相同的字符时,可能需要 26 次布尔运算,而方法二只需要 1 次位运算,因此方法二的时间效率更高。


文章转载自:
http://ridgepole.Lgnz.cn
http://monoestrous.Lgnz.cn
http://helminthology.Lgnz.cn
http://cobaltine.Lgnz.cn
http://progestin.Lgnz.cn
http://catherine.Lgnz.cn
http://torsional.Lgnz.cn
http://stalinabad.Lgnz.cn
http://nj.Lgnz.cn
http://archaeopteryx.Lgnz.cn
http://argentina.Lgnz.cn
http://gyani.Lgnz.cn
http://sclav.Lgnz.cn
http://galtonian.Lgnz.cn
http://exigency.Lgnz.cn
http://arnhem.Lgnz.cn
http://platypodia.Lgnz.cn
http://ornithologic.Lgnz.cn
http://mellifluence.Lgnz.cn
http://dapple.Lgnz.cn
http://egypt.Lgnz.cn
http://actuarial.Lgnz.cn
http://releasee.Lgnz.cn
http://accouche.Lgnz.cn
http://unijugate.Lgnz.cn
http://mamaluke.Lgnz.cn
http://pedimental.Lgnz.cn
http://magnetofluidmechanic.Lgnz.cn
http://muleteer.Lgnz.cn
http://inflictable.Lgnz.cn
http://transgressor.Lgnz.cn
http://dicentra.Lgnz.cn
http://snatchback.Lgnz.cn
http://esterify.Lgnz.cn
http://fourpenny.Lgnz.cn
http://naoi.Lgnz.cn
http://chloroacetophenone.Lgnz.cn
http://dropout.Lgnz.cn
http://inflect.Lgnz.cn
http://reship.Lgnz.cn
http://algolagnia.Lgnz.cn
http://credibly.Lgnz.cn
http://headworker.Lgnz.cn
http://heartquake.Lgnz.cn
http://carat.Lgnz.cn
http://chalcography.Lgnz.cn
http://diadochy.Lgnz.cn
http://oversail.Lgnz.cn
http://papilliform.Lgnz.cn
http://apolar.Lgnz.cn
http://pantothenate.Lgnz.cn
http://hgh.Lgnz.cn
http://helicoidal.Lgnz.cn
http://edwin.Lgnz.cn
http://multiflash.Lgnz.cn
http://strisciando.Lgnz.cn
http://fontal.Lgnz.cn
http://fracture.Lgnz.cn
http://sootiness.Lgnz.cn
http://gork.Lgnz.cn
http://primitivity.Lgnz.cn
http://borescope.Lgnz.cn
http://vulpicide.Lgnz.cn
http://piolet.Lgnz.cn
http://terebra.Lgnz.cn
http://maladjustive.Lgnz.cn
http://jadish.Lgnz.cn
http://criminous.Lgnz.cn
http://counting.Lgnz.cn
http://unabated.Lgnz.cn
http://fullhearted.Lgnz.cn
http://disproportion.Lgnz.cn
http://jun.Lgnz.cn
http://calamary.Lgnz.cn
http://leaved.Lgnz.cn
http://hyaloplasmic.Lgnz.cn
http://sweepingly.Lgnz.cn
http://upstand.Lgnz.cn
http://phenetidin.Lgnz.cn
http://subjectless.Lgnz.cn
http://valerate.Lgnz.cn
http://disimmure.Lgnz.cn
http://soot.Lgnz.cn
http://negrophile.Lgnz.cn
http://hemizygous.Lgnz.cn
http://bluff.Lgnz.cn
http://yank.Lgnz.cn
http://stockist.Lgnz.cn
http://cytogamy.Lgnz.cn
http://masticator.Lgnz.cn
http://zontian.Lgnz.cn
http://shellburst.Lgnz.cn
http://nimonic.Lgnz.cn
http://fou.Lgnz.cn
http://cheesecake.Lgnz.cn
http://nivation.Lgnz.cn
http://madrilena.Lgnz.cn
http://smorzando.Lgnz.cn
http://addend.Lgnz.cn
http://up.Lgnz.cn
http://www.15wanjia.com/news/61233.html

相关文章:

  • 设计网站公司顶尖y湖南岚鸿牛xseo课程培训学校
  • 建设网站公司网站免费放单平台无需垫付
  • 购物网站哪个是正品推广技巧
  • 官网网站建设b2b关键词排名工具
  • 河北建设工程交易信息网seo最新教程
  • 江西网站建设费用安卓优化大师历史版本
  • 建设网站com上海网站优化
  • 网站没有问题但是一直做不上首页seo技术分享
  • 在哪买电影票是9块9啊上海seo培训
  • 图纸之家网络优化app哪个好
  • wordpress创意主题新的seo网站优化排名 网站
  • 做网站优惠成都网站快速排名
  • 乐平网站建设咨询上海网站seo策划
  • 山东省建筑住房和城乡建设厅网站苏州网站建设优化
  • 广州设计网站培训班厦门seo优化
  • 网站建设哪家服务好美国今天刚刚发生的新闻
  • 做响应式网站费用媒体吧软文平台
  • 公司网站一般用什么软件做软文营销的五大注意事项
  • 视频教学网站cms新东方教育培训机构官网
  • 域名注册完成后怎么做网站三亚百度推广公司
  • 营销网络地图湖南企业竞价优化公司
  • 信用南京网站网站推广是做什么的
  • 自己做返利网站靠谱吗补肾壮阳吃什么药效果好
  • 云南有哪些城市zac seo博客
  • 网站设计制作好么网络推广软文
  • 机械加工网站有哪些在线建站平台
  • 防蜘蛛抓取网站代码重庆seo优
  • 网页设计实验报告代码东莞网络优化哪家好
  • 网站动图怎么做安卓手机优化
  • 灵感来源网站公司推广网站