当前位置:网站首页>[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
2022-07-06 09:18:00 【邓嘉文Jarvan】
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
题目1:
给定一个字符串数组 words,请计算当两个字符串 words[i] 和 words[j] 不包含相同字符时,它们长度的乘积的最大值。假设字符串中只包含英语的小写字母。如果没有不包含相同字符的一对字符串,返回 0。
示例 1:
输入: words = [“abcw”,“baz”,“foo”,“bar”,“fxyz”,“abcdef”]
输出: 16
解释: 这两个单词为 “abcw”, “fxyz”。它们不包含相同字符,且长度的乘积最大。
示例 2:
输入: words = [“a”,“ab”,“abc”,“d”,“cd”,“bcd”,“abcd”]
输出: 4
解释: 这两个单词为 “ab”, “cd”。
示例 3:
输入: words = [“a”,“aa”,“aaa”,“aaaa”]
输出: 0
解释: 不存在这样的两个单词。
提示:
2 <= words.length <= 1000
1 <= words[i].length <= 1000
words[i] 仅包含小写字母
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/aseY1I
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路1:
字母的个数只有 26 个,我们可以用一个 int 32 的位来记录该单词是否含有该字母
比如
“ac”0101
ac:最后1位代表a存在,倒数第二位0代表b不存在
“bd”1010
ac的二进制 & bd的二进制如果为0,就证明不相同, 反之相同
- 将word转化为二进制时间复杂度 O(n*k)
- 使用 & 运算找到没有重复数字的 word 计算长度刷新长度时间复杂度 O(n2)
代码
func maxProduct(words []string) int {
//思路:
// * 将word转化为二进制时间复杂度 O(n*k)
// * 使用 & 运算找到没有重复数字的 word 计算长度刷新长度时间复杂度 O(n2)
//参数处理
if len(words) < 2 {
return 0
}
//将word转化为二进制时间复杂度 O(n*k)
nums := make([]int,len(words))
for i:=0;i<len(words);i++ {
for j:=0;j<len(words[i]);j++{
nums[i] |= (1<<(words[i][j] - 'a'))
}
}
//使用 & 运算找到没有重复数字的 word 计算长度刷新长度时间复杂度 O(n2)
max := 0
for i:=0;i<len(words);i++ {
for j:=i + 1;j<len(words);j++{
if nums[i] & nums[j] == 0 {
tempMax := len(words[i]) * len(words[j])
if tempMax > max {
max = tempMax
}
}
}
}
return max
}
测试
边栏推荐
- Expected value (EV)
- InnoDB dirty page refresh mechanism checkpoint in MySQL
- 程序设计大作业:教务管理系统(C语言)
- Esp8266 connect onenet (old mqtt mode)
- (5) Introduction to R language bioinformatics -- ORF and sequence analysis
- Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
- Vulnhub target: hacknos_ PLAYER V1.1
- 编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)
- KF UD分解之UD分解基础篇【1】
- Lean product development - Lean Software Development & lean product development
猜你喜欢
There is no red exclamation mark after SVN update
Teach you to release a DeNO module hand in hand
Halcon knowledge: gray_ Tophat transform and bottom cap transform
NRF24L01 troubleshooting
Latex learning
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
FairyGUI复选框与进度条的组合使用
(core focus of software engineering review) Chapter V detailed design exercises
C programming exercise
Excel导入,导出功能实现
随机推荐
【GNSS】抗差估计(稳健估计)原理及程序实现
如何给Arduino项目添加音乐播放功能
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
JUC forkjoin and completable future
Idea problem record
Database course design: college educational administration management system (including code)
Office prompts that your license is not genuine pop-up box solution
MySQL shutdown is slow
[offer18] delete the node of the linked list
單片機藍牙無線燒錄
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
[899] ordered queue
2021.11.10 compilation examination
First use of dosbox
Programming homework: educational administration management system (C language)
Unity3D制作注册登录界面,并实现场景跳转
Servlet
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
Unity3d, Alibaba cloud server, platform configuration