使用位运算存储每个字符串,这样可以在O(n)时间内预处理完所有字符串,接着O(n^2)时间复杂度内两重循环来两两比较,如果两个串相与为0,说明位上没有同时为1的情况,即没有相同字母。
- class Solution {
- public:
- int bitset(string str){
- int res = 0;
- for(int i=0;i
length();i++){ - res |= 1<<(str[i]-'a');
- }
- return res;
- }
-
- int maxProduct(vector
& words) { - int res = 0;
- int n = words.size();
- vector<int>bits(n);
- for(int i=0;i
- bits[i] = bitset(words[i]);
- }
- for(int i=0;i
- cout<
- for(int j=i+1;j
- if( (bits[i] & bits[j]) == 0 ){
- res = max(res,int(words[i].length()*words[j].length()));
- }
- }
- }
- return res;
- }
- };
-
相关阅读:
【成为红帽工程师】第二天 ssh远程连接服务器
【Ubuntu】Ubuntu20.04安装EasyConnect后打不开的问题。
普林斯顿微积分读本第一章--函数、反函数
单片机-控制按键点亮LED灯
AI斗地主-按键精灵
格利尔在北交所上市:市值突破9亿元,朱从利夫妇为实控人
缓存相关问题
如何封装一个实用的上传组件
android中集成阿里云金融级实人认证
odoo13 升级odoo15时注意点
-
原文地址:https://blog.csdn.net/liangcha_xyy/article/details/134274103