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 ...
随机推荐
- 程序猿必备软件转载自 www.uhdesk.com
XMLSpy 2012 企业版中文破解版 软件描写叙述: XMLSpy是XML(标准通用标记语言的子集)编辑器,支持WYSWYG.支持Unicode.多字符集,支持Well-formed和Valida ...
- outlook vba 2
- 读取Webpage表中的内容 分类: H3_NUTCH 2015-02-10 14:59 418人阅读 评论(0) 收藏
nutch将从网页中抓取到的信息放入hbase数据库中,默认情况下表名为$crawlId_webpage,但表中的内容以16进制进行表示,直接scan或者通过Java API进行读取均只能读取到16进 ...
- 钢琴 - steinway
http://www.wangyanpiano.com/bbs/thread-104723-1-1.html Poston 波士顿钢琴 三角钢琴 GP-156 黑色抛光 178 ...
- Swift 语言概览 -自己在Xcode6 动手写2-tableView
import UIKit class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource { va ...
- ASP.NET MVC中实现多个button提交的几种方法
有时候会遇到这样的情况:在一个表单上须要多个button来完毕不同的功能,比方一个简单的审批功能. 假设是用webform那不须要讨论,但asp.net mvc中一个表单仅仅能提交到一个Action处 ...
- ie7easyui的书写要规范
在书写easyui对象的属性,有时候习惯在,属性的末尾再加一个“,”,这个在高版本浏览器是没事的,但是在ie7及以下,会有报错
- .NET Core微服务之路:不断更新中的目录 (v0.43)
原文:.NET Core微服务之路:不断更新中的目录 (v0.43) 微服务架构,对于从事JAVA架构的童鞋来说,早已不是什么新鲜的事儿,他们有鼎鼎大名的Spring Cloud这样的全家桶框架支撑, ...
- 2015年工作中遇到的问题:71-80,Tomcat-Redis-浮点数-HTTPS
71.Tomcat访问项目带了"项目名称".最简单的办法,是把这个项目部署到"root"目录,据boss所说,阿里的每一个项目,都单独放到1个Tomcat的ro ...
- [Angular] Dynamic component's instance and sorting
After create a component dynamic, we are able to change the component's props and listen to its even ...