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。

 1 class Solution {
 2 public:
 3     int maxProduct(std::vector<std::string>& words)        {
 4         std::vector<int> flg_;
 5         for (std::vector<std::string>::size_type i = 0; i < words.size(); ++i){
 6             int tflg_ = 0;
 7             for (std::string::size_type j = 0; j < words[i].size(); ++j){
 8                 tflg_ |= (1 << (words[i][j] - ‘a‘));                    // 对于‘a‘ 就是 1 而对于‘b’是 10 ‘c‘是100
 9             }
10             flg_.push_back(tflg_);
11         }
12         int ans = 0;
13         for (std::vector<int>::size_type i = 0; i < flg_.size(); ++i){
14             for (std::vector<int>::size_type j = i + 1; j < flg_.size(); ++j){
15                 if ((flg_[i] & flg_[j]) == 0) ans = std::max(ans, (int)(words[i].size() * words[j].size()));//words[i]和words[j]中没有相同的字母
16             }
17         }
18         return ans;
19     }
20 };
时间: 2024-10-27 00:59:57

Leetcode 318 Maximum Product of Word Lengths 字符串处理+位运算的相关文章

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 two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Example

318. Maximum Product of Word Lengths

题目: Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Exa

318. Maximum Product of Word Lengths java solutions

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Example

318 Maximum Product of Word Lengths 最大单词长度乘积

给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返回 0.示例 1:输入 ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]返回 16两个单词可以为 "abcw", "

318. Maximum Product of Word Lengths ——本质:英文单词中字符是否出现可以用26bit的整数表示

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Example

【leetcode】Maximum Product of Word Lengths

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Example

Maximum Product of Word Lengths

Maximum Product of Word Lengths Total Accepted: 750 Total Submissions: 2060 Difficulty: Medium Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume tha

Maximum Product of Word Lengths -- LeetCode

Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0. Example

leetcode笔记:Maximum Product of Word Lengths

一. 题目描述 Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case letters. If no such two words exist, return 0.