Leetcode 318 Maximum Product of Word Lengths 字符串处理+位运算
先介绍下本题的题意:
在一个字符串组成的数组words中,找出max{Length(words[i]) * Length(words[j]) },其中words[i]和words[j]中没有相同的字母,在这里字符串由小写字母a-z组成的。
对于这道题目我们统计下words[i]的小写字母a-z是否存在,然后枚举words[i]和words[j],找出max{Length(words[i]) * Length(words[j]) }。
小写字母a-z是26位,一般统计是否存在我们要申请一个bool flg[26]这样的数组,但是我们在这里用int代替,int是32位可以替代flg数组,用 与(&),或(1),以及向左移位(<<)就能完成。如“abcd” 的int值为 0000 0000 0000 0000 0000 0000 0000 1111,“wxyz” 的int值为 1111 0000 0000 0000 0000 0000 0000 0000,这样两个进行与(&)得到0, 如果有相同的字母则不是0。
class Solution {
public:
int maxProduct(std::vector<std::string>& words)
{
std::vector<int> flg_;
for (std::vector<std::string>::size_type i = ; i < words.size(); ++i){
int tflg_ = ;
for (std::string::size_type j = ; j < words[i].size(); ++j){
tflg_ |= ( << (words[i][j] - 'a')); // 对于'a' 就是 1 而对于‘b’是 10 'c'是100
}
flg_.push_back(tflg_);
}
int ans = ;
for (std::vector<int>::size_type i = ; i < flg_.size(); ++i){
for (std::vector<int>::size_type j = i + ; j < flg_.size(); ++j){
if ((flg_[i] & flg_[j]) == ) ans = std::max(ans, (int)(words[i].size() * words[j].size()));//words[i]和words[j]中没有相同的字母
}
}
return ans;
}
};
Leetcode 318 Maximum Product of Word Lengths 字符串处理+位运算的更多相关文章
- leetcode 318. Maximum Product of Word Lengths
传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...
- Java [Leetcode 318]Maximum Product of Word Lengths
题目描述: Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where ...
- leetcode@ [318] Maximum Product of Word Lengths (Bit Manipulations)
https://leetcode.com/problems/maximum-product-of-word-lengths/ Given a string array words, find the ...
- [leetcode]318. Maximum Product of Word Lengths单词长度最大乘积
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- LeetCode 318. Maximum Product of Word Lengths (状态压缩)
题目大意:给出一些字符串,找出两个不同的字符串之间长度之积的最大值,但要求这两个字符串之间不能拥有相同的字符.(字符只考虑小写字母). 题目分析:字符最多只有26个,因此每个字符串可以用一个二进制数来 ...
- 【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 位运算 日期 题目地址:https://le ...
- LeetCode 【318. Maximum Product of Word Lengths】
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- 318 Maximum Product of Word Lengths 最大单词长度乘积
给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...
- 318. Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
随机推荐
- or小计
1.使用or的时候,必须养成两边添加括号,否则结果完全不一样. 2.or条件如果复杂的情况下,可以适当考虑union all改写.
- MongoDB 管理
1.给数据库增加分片功能 mongos> use admin mongos> db.runCommand({enablesharding:"cipnet"}) mong ...
- swift学习第六天:数组
数组 数组的介绍 数组(Array)是一串有序的由相同类型元素构成的集合 数组中的集合元素是有序的,可以重复出现 Swift中的数组 swift数组类型是Array,是一个泛型集合 数组的初始化 数组 ...
- 广播(broadcast)、电视与电视网络
1. 闭路电视与电视 Closed Circuit Television (CCTV) 一种图像通信系统.其信号从源点只传给预先安排好的与源点相通的特定电视机.广泛用于大量不同类型的监视工作.教育.电 ...
- VS(Visual Studio)自动创建的文件格式
.sln:solution,解决方案文件: .vsxproj:解决方案下的项目文件: .vssettings:环境设置文件, 菜单栏 ⇒ [工具]⇒ [导入和导出设置]⇒ 进行环境设置的导入和导出操作 ...
- CRT(C Runtime Library)—— C/C++运行时库
C runtime library(part of the C standard library) 任何一个 C 程序,它的背后都有一套庞大的代码来进行支撑,使得该程序得以运行在更高级别上,而不必担心 ...
- adobe Changing PDF Link Color for Regulatory Submissions
Changing PDF Link Color for Regulatory Submissions http://blogs.adobe.com/acrobatforlifesciences/200 ...
- Js数组排序函数:sort()
原文链接:https://blog.csdn.net/qq_37936542/article/details/78979521 js原生的 sort() 排序函数使用起来很方便 语法:arrayObj ...
- Java程序猿的JavaScript学习笔记(5——prototype和Object内置方法)
计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...
- JAVA类(下)
我看完了Java类,与C++相比,复杂了一点.其中有类的嵌套定义即内部类,枚举类等. 我看这两节花了我很多时间.其中有一些概念还是有点难懂. 下面,我详细总结内部类与枚举类. 内部类 内部类的主要作用 ...