使用位运算存储每个字符串,这样可以在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;
- }
- };
-
相关阅读:
华为策略流控
kubernetes的DevOps业务(二):Jenkins,GitLab,Harbor,Tekton,GitOps
IP包头分析
机器学习和数据科学的最佳公共数据集机器学习、数据科学、情感分析、计算机视觉、自然语言处理 (NLP)、临床数据等的最佳公共数据集。
DO280OpenShift访问控制--加密和ConfigMap
C语言中的结构体
动态修改日志级别,太有用了!
纸牌游戏设计制作《摸鱼2》(C语言)
高性能服务器之Reactor设计
easyrecovery15数据恢复软件收费吗?功能强大吗?
-
原文地址:https://blog.csdn.net/liangcha_xyy/article/details/134274103
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU