[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 ...
随机推荐
- centos svn 的搭建
一. SVN 简介 Subversion(SVN) 是一个开源的版本控制系統, 也就是说 Subversion 管理着随时间改变的数据. 这些数据放置在一个中央资料档案库(repository) 中. ...
- 最全的CSS浏览器兼容问题http://www.68design.net/Web-Guide/HTMLCSS/37154-1.html
最全的CSS浏览器兼容问题 来源:68design.net 作者:邓飞飞 2008年09月23日 14:17 网友评论:7条 点击:71865 CSS 对浏览器的兼容性有时让人很头疼,或许当你了解当中 ...
- MyBatis 中#与$的区别
今天在工作中有个点击排序的功能调试了许久,终寻因,总结之. 需求是这样的,页面有个table,有一列的上下箭头可点击并排序.对于这种需求,我的mybatis.xml的sql配置写成了如下: < ...
- 并发基础(十) 线程局部副本ThreadLocal之正解
本文将介绍ThreadLocal的用法,并且指出大部分人对ThreadLocal 的误区. 先来看一下ThreadLocal的API: 1.构造方法摘要 ThreadLocal(): 创建一个线程 ...
- 知乎日报 API的图片盗链问题
由最近 基于vue的知乎日报单页应用 引发的问题 以及问题解决历程 通过 知乎日报API 基于vue做一个知乎日报的单页应用,在获取图片时存在一个图片盗链问题,图片无法加载 提示 403 错误, 最终 ...
- jpa 多对多
entity Item package entity; import java.util.HashSet; import java.util.Set; import javax.persisten ...
- JAVA 读取配置文件 xxx.properties
package config_demo; import java.io.InputStream; import java.util.Properties; public class UrlDemo { ...
- Mysql 事件event_scheduler是OFF
1 在查询窗口执行: SHOW VARIABLES LIKE 'event_scheduler' 查看是OFF 还是ON; 方式1: 修改.int配置文件 添加一行: event_scheduler ...
- as3 arguments.callee与... (rest)
import flash.display.Sprite; var count:int = 1; ArgumentsExample() function ArgumentsExample() { fir ...
- jetty异常
异常一: java.net.BindException: Address already in use: bind jvm 1 | 2017-10-18 15:08:10,792+0800 WARN ...