当前位置:网站首页>leetcode-318.最大单词长度乘积
leetcode-318.最大单词长度乘积
2022-08-02 05:15:00 【KGundam】
位运算
题目详情
给你一个字符串数组 words
,找出并返回 length(words[i]) * length(words[j])
的最大值,并且这两个单词不含有公共字母。如果不存在这样的两个单词,返回 0 。
示例1:
输入:words = ["abcw","baz","foo","bar","xtfn","abcdef"]
输出:16
解释:这两个单词为 "abcw", "xtfn"。
示例2:
输入:words = ["a","ab","abc","d","cd","bcd","abcd"]
输出:4
解释:这两个单词为 "ab", "cd"。
示例3:
输入:words = ["a","aa","aaa","aaaa"]
输出:0
解释:不存在这样的两个单词。
思路:
本题的关键显然是判断两个单词是否有相同字母,如果遍历两两比较,显然复杂度较高
我们可以利用位运算,因为题目要求两个无相同字母的最长的长度乘积,所以我们可以建立一个hash
利用位左移运算得到每个字母独特的1的位置(int类型长度为32,字母一共26个,所以位置足够用)
再利用或运算|=将其赋予mask,最后每个单词都会有自己的一个独特的mask
也有可能存在一模一样mask的单词(存在重复字母比如meet和met的mask就相同)
我们只需要每次更新mask对应的length为较长的即可
在我们遍历一个个单词的时候,遍历完一个我们就用这个mask和hash里已存的mask按位与
当按位与为0时就更新ans为较长的乘积,最后我们遍历完单词,也求出了最终答案
我的代码:
class Solution
{
public:
int maxProduct(vector<string>& words)
{
unordered_map<int, int> hash; //建立hash存储字符串
int ans = 0;
for (const string & word : words) //对于每个单词
{
int mask = 0, size = word.size();
for (const char & c : word) //每个单词的每个字母
{
//c-'a'就是c到a的距离,然后将1左移这么多距离
//就能将000..01这个1移到专属于c的一个位置
//然后利用mask与它做或运算,从而将这个位置的1
//赋给mask,最后遍历完单词每个字母,就会得到一个独特的总数
mask |= 1 << (c - 'a');
}
//如果有同mask(有重复字母)的词我们取较长的
//例如meet和met具有相同的mask,我们取length就是4
hash[mask] = max(hash[mask], size);
//遍历hash寻找满足的h_mask
for (const auto& [h_mask, h_len]: hash)
{
if (!(mask & h_mask)) //按位与如果不存在相同字母
{
ans = max(ans, size * h_len);//就更新ans为较大值
}
}
}
return ans;
}
};
位运算常用技巧
边栏推荐
- Double for loop case (use js jiujiu printing multiplication table)
- C语言入门实战(13):十进制数转二进制
- The Go language learning notes - dealing with timeout - use the language from scratch from Context
- 25K test old bird's 6-year experience in interviews, four types of companies, four types of questions...
- 6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
- 软件测试的需求人才越来越多,为什么大家还是不太愿意走软件测试的道路?
- H5 access payment process - WeChat payment & Alipay payment
- Home NAS server (4) | MergerFS and SnapRaid data backup
- Detailed explanation of interface in Go language
- 回文串求解的进阶方法
猜你喜欢
Say good woman programmers do testing have an advantage?More than a dozen interview, abuse of cry ~ ~ by the interviewer
Redis集群模式
6W+字记录实验全过程 | 探索Alluxio经济化数据存储策略
虚拟现实房产展示系统提前预见未来装修效果
双重for循环案例(用js打印九九乘法表)
上海交大牵手淘宝成立媒体计算实验室:推动视频超分等关键技术发展
Redis-cluster mode (master-slave replication mode, sentinel mode, clustering mode)
软件测试在职2年跳槽4次,你还在怪老板不给你涨薪?
nacos注册中心
Polar Parametrization for Vision-based Surround-View 3D Detection 论文笔记
随机推荐
Integrate ssm (1)
提高软件测试能力的方法有哪些?看完这篇文章让你提升一个档次
The virtual reality real estate display system foresees the future decoration effect in advance
What are the ways to improve software testing capabilities?After reading this article, it will take you up a notch
说好的女程序员做测试有优势?面试十几家,被面试官虐哭~~
Say good woman programmers do testing have an advantage?More than a dozen interview, abuse of cry ~ ~ by the interviewer
为什么4个字节的float要比8个字节的long大呢?
点云旋转到参考坐标系方向(最小方向包围盒方法)
25K测试老鸟6年经验的面试心得,四种公司、四种问题…
程序员写PPT的小技巧
pytorch常用函数
构造方法、成员变量、局部变量
Polar Parametrization for Vision-based Surround-View 3D Detection Paper Notes
Detailed explanation of interface in Go language
路由规划中级篇
Deep learning - CNN realizes the recognition of MNIST handwritten digits
Introduction to Grid Layout
51 MCU peripherals: DS18B20
APP Bluetooth connection test of test technology
Redis集群模式