当前位置:网站首页>[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

边栏推荐
- Excel导入,导出功能实现
- Knowledge system of digital IT practitioners | software development methods -- agile
- Fabrication of fairygui simple Backpack
- How to improve the deletion speed of sequential class containers?
- GNSS定位精度指标计算
- 【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
- [offer29] sorted circular linked list
- 抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
- 2021.11.10汇编考试
- 3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
猜你喜欢

341. Flatten nested list iterator

Office提示您的许可证不是正版弹框解决

Design and implementation of general interface open platform - (39) simple and crude implementation of API services

Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)

Database course design: college educational administration management system (including code)
![[算法] 剑指offer2 golang 面试题10:和为k的子数组](/img/63/7422489d09a64ec9f0e79378761bf1.png)
[算法] 剑指offer2 golang 面试题10:和为k的子数组

Matlab读取GNSS 观测值o文件代码示例

Single chip Bluetooth wireless burning

基本Dos命令

FairyGUI复选框与进度条的组合使用
随机推荐
2021.11.10汇编考试
Fairygui loop list
Excel导入,导出功能实现
【无标题】
【RTKLIB 2.4.3 b34 】版本更新简介一
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
FairyGUI条子家族(滚动条,滑动条,进度条)
Teach you to release a DeNO module hand in hand
[算法] 剑指offer2 golang 面试题7:数组中和为0的3个数字
[leetcode622] design circular queue
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
Unity3d makes the registration login interface and realizes the scene jump
[Nodejs] 20. Koa2 onion ring model ----- code demonstration
KF UD分解之伪代码实现进阶篇【2】
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
MySQL error warning: a long semaphore wait
Database course design: college educational administration management system (including code)
Compile GDAL source code with nmake (win10, vs2022)
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique