[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 1:
Input: ["abcw","baz","foo","bar","xtfn","abcdef"]
Output: 16
Explanation: The two words can be "abcw", "xtfn".
Example 2:
Input: ["a","ab","abc","d","cd","bcd","abcd"]
Output: 4
Explanation: The two words can be "ab", "cd".
题意:
给定一堆单词,要求找出俩单词长度的最大乘积,要求俩单词不能有相同字母。
思路:
判断noOverLapping: 用2个set分别标记俩单词, 扫一遍set,若发现某个字母被同时标记过,则有重叠。
取最大乘积长度:两重for循环,两两比较,更新最大值。
代码:
class Solution {
public int maxProduct(String[] words) {
int result = 0;
for (int i = 0; i < words.length; ++i) {
for (int j = i + 1; j < words.length; ++j) {
int tmp = words[i].length() * words[j].length();
if ( noOverLapping(words[i], words[j])&& tmp > result) {
result = tmp;
}
}
}
return result;
}
private boolean noOverLapping(String a , String b){
boolean[] setA = new boolean[256];
boolean[] setB = new boolean[256]; for(int i = 0; i < a.length(); i++){
setA[a.charAt(i)] = true;
} for(int i = 0; i < b.length(); i++){
setB[b.charAt(i)] = true;
} for(int i = 0; i < 256; i++){
if(setA[i] == true && setB[i] == true){
return false;
}
} return true;
}
}
可以进一步优化:对于如何判断俩单词有没有相同字母,可用位向量表示每个字母是否出现即可,俩位向量异或即可得出是否有相同字母。
public class Solution {
public int maxProduct(String[] words) {
final int n = words.length;
final int[] hashset = new int[n]; for (int i = 0; i < n; ++i) {
for (int j = 0; j < words[i].length(); ++j) {
hashset[i] |= 1 << (words[i].charAt(j) - 'a');
}
} int result = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int tmp = words[i].length() * words[j].length();
if ((hashset[i] & hashset[j]) == 0 && tmp > result) {
result = tmp;
}
}
}
return result;
}
}
[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 ...
- [LeetCode] 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 (Bit Manipulations)
https://leetcode.com/problems/maximum-product-of-word-lengths/ Given a string array words, find the ...
- 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 (状态压缩)
题目大意:给出一些字符串,找出两个不同的字符串之间长度之积的最大值,但要求这两个字符串之间不能拥有相同的字符.(字符只考虑小写字母). 题目分析:字符最多只有26个,因此每个字符串可以用一个二进制数来 ...
- Leetcode 318 Maximum Product of Word Lengths 字符串处理+位运算
先介绍下本题的题意: 在一个字符串组成的数组words中,找出max{Length(words[i]) * Length(words[j]) },其中words[i]和words[j]中没有相同的字母 ...
- 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])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...
- 【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 位运算 日期 题目地址:https://le ...
随机推荐
- servlet的讲解
https://www.ibm.com/developerworks/cn/java/j-lo-servlet/
- HttpURLConnection连接超时问题
1.问题描述 这几天测试重构后的下载框架,发现在下载过程中如果网络中断或网络较差,个别应用的下载就会阻塞卡住,一直卡在 “正在下载 xx%”. 2.问题排查和定位 思考:网络差不应该报网络异常的错 ...
- windows下面安装easy_install和pip教程
方便安装whl:安装完成后,可以使用pip install xxx.whl 安装一个python轮子 python扩展库的路径:Python\Python36\Lib\site-packages\ ...
- mono部分源码解析
一.源码结构 这里仅列举几个重要的目录:mcs: mcs: Mono实现的基于Ecma标准的C#编译器. class: CLI的C#级的实现.类似于Android中的Java层,应用程序看 ...
- 【Codeforces】CF 165 E Compatible Numbers(状压dp)
题目 传送门:QWQ 分析 很难想到方向,但有方向了就很easy了. 我们如何减少不必要的计算? 如果我们知道了$ 100111 $的相容的数,$ 100101 $的相容数和他是完全一样的. 我们就靠 ...
- 使用minGW/cygwin在Windows是用于gcc开发
刚才记录了下用eclipse在linux下开发,突然想起来也另一种方法:MinGW. MinGW是Windows的gcc开发工具,直接使用Windows的运行库,所以可以在windows下面方便的用g ...
- LRU的理解与Java实现
简介 LRU(Least Recently Used)直译为"最近最少使用".其实很多老外发明的词直译过来对于我们来说并不是特别好理解,甚至有些词并不在国人的思维模式之内,比如快速 ...
- python装饰器(docorator)详解
引言: 装饰器是python面向对象编程三大器之一,另外两个迭代器.生成器只是我现在还没有遇到必须使用的场景,等确实需要用到的时候,在补充资料:装饰器在某些场景真的是必要的,比如定义了一个类或者一个函 ...
- 63. sqlserver查版本号问题
SELECT @@VERSION as 版本详细情况 SELECT SERVERPROPERTY('edition') as 软件版本 SELECT SERVERPROPERTY('ProductVe ...
- 38. CentOS-6.3安装配置Tomcat-7
安装说明 安装环境:CentOS-6.3安装方式:源码安装 软件:apache-tomcat-7.0.29.tar.gz下载地址:http://tomcat.apache.org/download-7 ...