当前位置:网站首页>[算法] 剑指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
}
测试

边栏推荐
- 1041 be unique (20 points (s)) (hash: find the first number that occurs once)
- 基本Dos命令
- Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
- 基于rtklib源码进行片上移植的思路分享
- Prove the time complexity of heap sorting
- Unity scene jump and exit
- 燕山大学校园网自动登录问题解决方案
- FairyGUI增益BUFF数值改变的显示
- 【干货】提升RTK模糊度固定率的建议之周跳探测
- NRF24L01 troubleshooting
猜你喜欢

Force buckle 1189 Maximum number of "balloons"

使用rtknavi进行RT-PPP测试

First use of dosbox

(1) Introduction Guide to R language - the first step of data analysis

单片机蓝牙无线烧录

Fairygui gain buff value change display

Unity3D,阿里云服务器,平台配置

Unity scene jump and exit

程序设计大作业:教务管理系统(C语言)

Fabrication d'un sac à dos simple fairygui
随机推荐
Acwing-116 pilot brother
Unity3D,阿里云服务器,平台配置
HCIP Day 12
Programming homework: educational administration management system (C language)
NovAtel 板卡OEM617D配置步骤记录
Containers and Devops: container based Devops delivery pipeline
MySQL performance tuning - dirty page refresh
Idea problem record
Teach you to release a DeNO module hand in hand
FairyGUI条子家族(滚动条,滑动条,进度条)
Agile development helps me
What are the advantages of using SQL in Excel VBA
Fairygui character status Popup
What is the maximum length of MySQL varchar field
Fairygui loop list
[offer18] delete the node of the linked list
(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
Unity3D制作注册登录界面,并实现场景跳转
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
数据库课程设计:高校教务管理系统(含代码)