11-17 Leetcode318. 最大单词长度乘积
2021/11/17 23:14:07
本文主要是介绍11-17 Leetcode318. 最大单词长度乘积,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Leetcode318. 最大单词长度乘积
思路:位运算
如何判断不同单词字母重复?题目的提示是只用小写字母,保证所有的字母最多只有26个,使用 i n t int int前26位保存每一个字母是否出现过,使用 h a s h hash hash存所有单词对应的特征,两次循环遍历哈希表
class Solution { public: int maxProduct(vector<string>& words) { int n = words.size(); unordered_map<int, string> hash; for (auto& str : words) { int m = str.size(), t = 0; for (auto& c : str) { int u = c - 'a'; t |= (1 << u); } if (!hash.count(t) || hash[t].size() < m) hash[t] = str; } int res = 0; for (auto& [n, str1] : hash) for (auto &[m, str2]: hash) if ((n & m) == 0) { int x = str1.size() * str2.size(); if (x > res) { res = x; } } return res; } };
这篇关于11-17 Leetcode318. 最大单词长度乘积的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-07fastcgi 是什么-icode9专业技术文章分享
- 2024-10-07fastcgi 的详细使用教程介绍-icode9专业技术文章分享
- 2024-10-07git如何更新单个文件到本地-icode9专业技术文章分享
- 2024-10-07如何使用ASM(Abstract Syntax Tree Manipulation)技术来修改第三方AAR依赖中的函数-icode9专业技术文章分享
- 2024-10-07Activity 跳转时间耗时很长怎么优化解决-icode9专业技术文章分享
- 2024-10-07Androud Toast 有哪些常用的第三方组件-icode9专业技术文章分享
- 2024-10-07在viewmodel中怎么使用 mmkv?-icode9专业技术文章分享
- 2024-10-07MMKV.defaultMMKV() 是单例模式吗?-icode9专业技术文章分享
- 2024-10-04el-table 开启定时器下,表格的选中状态会消失是什么原因-icode9专业技术文章分享
- 2024-10-03如何安装和初始化飞牛私有云 fnOS?-icode9专业技术文章分享