当前位置:网站首页>[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length
2022-07-06 12:51:00 【Deng Jiawen jarvan】
[ Algorithm ] The finger of the sword offer2 golang Interview questions 5: Maximum product of word length
subject 1:
Given an array of strings words, Please calculate when two strings words[i] and words[j] Does not contain the same characters , The maximum of the product of their lengths . Suppose that the string contains only English lowercase letters . If there is no pair of strings that do not contain the same characters , return 0.
Example 1:
Input : words = [“abcw”,“baz”,“foo”,“bar”,“fxyz”,“abcdef”]
Output : 16
explain : These two words are “abcw”, “fxyz”. They do not contain the same characters , And the product of length is the largest .
Example 2:
Input : words = [“a”,“ab”,“abc”,“d”,“cd”,“bcd”,“abcd”]
Output : 4
explain : These two words are “ab”, “cd”.
Example 3:
Input : words = [“a”,“aa”,“aaa”,“aaaa”]
Output : 0
explain : There are no such two words .
Tips :
2 <= words.length <= 1000
1 <= words[i].length <= 1000
words[i] Only lowercase letters
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/aseY1I
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas 1:
The number of letters is only 26 individual , We can use one int 32 To record whether the word contains the letter
such as
“ac”0101
ac: Last 1 On behalf of a There is , Next to last 0 representative b non-existent
“bd”1010
ac Binary system & bd If the binary of is 0, Prove different , On the contrary, they are the same
- take word Convert to binary time complexity O(n*k)
- Use & The operation finds no duplicate numbers word Calculation length refresh length time complexity O(n2)
Code
func maxProduct(words []string) int {
// Ideas :
// * take word Convert to binary time complexity O(n*k)
// * Use & The operation finds no duplicate numbers word Calculation length refresh length time complexity O(n2)
// Processing parameters
if len(words) < 2 {
return 0
}
// take word Convert to binary time complexity 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'))
}
}
// Use & The operation finds no duplicate numbers word Calculation length refresh length time complexity 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
}
test
边栏推荐
- [Offer29] 排序的循环链表
- [Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
- (5) Introduction to R language bioinformatics -- ORF and sequence analysis
- Office提示您的许可证不是正版弹框解决
- [algorithme] swordfinger offer2 golang question d'entrevue 2: addition binaire
- [offer18] delete the node of the linked list
- How to add music playback function to Arduino project
- [算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
- [offer78] merge multiple ordered linked lists
- KF UD分解之UD分解基础篇【1】
猜你喜欢
Unity场景跳转及退出
(core focus of software engineering review) Chapter V detailed design exercises
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
【无标题】
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
Latex learning
FGUI工程打包发布&导入Unity&将UI显示出来的方式
Unity scene jump and exit
Derivation of logistic regression theory
FairyGUI按钮动效的混用
随机推荐
NovAtel 板卡OEM617D配置步骤记录
Single chip Bluetooth wireless burning
C programming exercise
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
平衡二叉树详解 通俗易懂
FairyGUI增益BUFF数值改变的显示
Unity scene jump and exit
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
Unity场景跳转及退出
程序设计大作业:教务管理系统(C语言)
[899]有序队列
燕山大学校园网自动登录问题解决方案
[offer9] implement queues with two stacks
rtklib单点定位spp使用抗差估计遇到的问题及解决
2021.11.10汇编考试
Devops' future: six trends in 2022 and beyond
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
微信小程序开发心得
[Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)