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 ... 
随机推荐
- 页面中如何引用外部的HTML(四种方法)
			页面中如何引用外部的HTML(四种方法) 一.总结 一句话总结:a.iframe标签 b.ajax引入代码片段 c.link import的方法导入 d.re ... 
- 14、USB摄像头(V4L2接口)的图片采集
			参考网站http://www.cnblogs.com/surpassal/archive/2012/12/19/zed_webcam_lab1.html 一.一些知识 1.V4L和V4L2. V4L是 ... 
- Engine工具栏按钮的使用详解
			转自原文 Engine自定义控件实现toolbar功能 Engine提供的工具条能够轻易实现各种操作,非常方便,可是不好的地方就是太死板了,toolbar的图标都不能改.因此需要自己做按钮做控件去实现 ... 
- iis windows phpstudy安装redis扩展
			说明,我的服务器是2008 64位 php5.4.33 首先下载符合条件的redis扩展,是否符合条件可以参考https://pecl.php.net/package/redis,进入之后,点击&qu ... 
- word多出空标题,样式是列出段落 - -显示时,选择不勾选“隐藏文字”
			word多出空标题,样式是列出段落 
- oracle 列授权相关测试
			create tablespace liangtbs datafile '/home/oradata/lgjdb/liangtbs01.dbf' size 50m autoextend on;crea ... 
- ssion机制详解
			ssion机制详解 ref:http://justsee.iteye.com/blog/1570652 虽然session机制在web应用程序中被采用已经很长时间了,但是仍然有很多人不清楚sess ... 
- windows 下安装git
			Git是当今最流行的版本控制软件,它包含了许多高级工具,这里小编就讲一下Git的安装. 首先如下图:(点击next) 第二步:文件位置存储,可根据自己盘的情况安装 第三步:安装配置文件,自己需要的都选 ... 
- Android 如何Android中自定义Navigationbar
			在如何控制Android系统中NavigationBar 的显示与隐藏文章里简要地介绍了Navigationbar的背景知识, NavigationBar的代码是放在... rameworksasep ... 
- Mac OSX 下配置 LNMP开发环境
			不久前负责了一个项目需要配置PHP7的开发环境,因为之前所有的项目用的是PHP5的,所以研究了这些东西,但是很遗憾,电脑出了问题,不得已重装了系统,然后你懂得...什么都没有了,要重新来过.. 虽然本 ... 
