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

边栏推荐
- GPS高程拟合抗差中误差的求取代码实现
- The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
- [leetcode19] delete the penultimate node in the linked list
- Mixed use of fairygui button dynamics
- PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
- Problèmes avec MySQL time, fuseau horaire, remplissage automatique 0
- FairyGUI增益BUFF数值改变的显示
- Esp8266 connect onenet (old mqtt mode)
- Talking about the startup of Oracle Database
- Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
猜你喜欢

NovAtel 板卡OEM617D配置步骤记录

Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports

Fabrication of fairygui simple Backpack

【干货】提升RTK模糊度固定率的建议之周跳探测

基本Dos命令

Latex learning

PR 2021 quick start tutorial, first understanding the Premiere Pro working interface

CUDA C programming authoritative guide Grossman Chapter 4 global memory

Unity3d, Alibaba cloud server, platform configuration

Database course design: college educational administration management system (including code)
随机推荐
RTKLIB: demo5 b34f.1 vs b33
Servlet
ESP8266连接onenet(旧版MQTT方式)
Programming homework: educational administration management system (C language)
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
[offer78]合并多个有序链表
使用rtknavi进行RT-PPP测试
FairyGUI复选框与进度条的组合使用
Fairygui gain buff value change display
NRF24L01故障排查
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
MySQL performance tuning - dirty page refresh
[offer18] delete the node of the linked list
Devops' future: six trends in 2022 and beyond
Intermediate use tutorial of postman [environment variables, test scripts, assertions, interface documents, etc.]
InnoDB dirty page refresh mechanism checkpoint in MySQL
Excel导入,导出功能实现
KF UD分解之UD分解基础篇【1】
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
Vulnhub target: hacknos_ PLAYER V1.1