[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 ...
随机推荐
- Mysql 於lampp xampp LinuxUbuntu下的配置
默认执行Lampp/Xampp 於Ubuntu下完成后,需要对mysql进行一系列的配置,方可进行更好的操作 lampp下的mysql配置文件路径: /opt/lampp/etc/my.cnf 1 配 ...
- keras输出中间层结果,某一层的权重、偏置
转载:https://blog.csdn.net/hahajinbu/article/details/77982721 from keras.models import Sequential,Mode ...
- linux常用命令解析
linux下一些注意事项 1. linux下严格区分大小写 ls 简述:列出文件或目录列表. -> ls 默认列出当前目录下的所有文件. -> ls -l(long)以长格式查看文件. - ...
- Java 迭代器 工具类
迭代器:Iterator接口 //获取集合中的对象Iterator<E> iterator() interface Iterator { boolean hasNext(); Object ...
- Dependency Injection in ASP.NET Web API 2 Using Unity
What is Dependency Injection? A dependency is any object that another object requires. For example, ...
- CSS 背景background实例
css背景background用于设置html标签元素的背景颜色.背景图片已经其他背景属性.本文章向码农介绍CSS 背景background使用方法和基本的使用实例.需要的码农可以参考一下. 一.Cs ...
- 线程池,queue模块增加用法
1 同一个进程内的队列(多线程) import queue queue.Queue() 先进先出 queue.LifoQueue() 后进先出 queue.PriorityQueue() 优先级队列 ...
- LaunchFaster 启动器工具 - 类似 Rolan 和音速启动的图标式快捷启动软件
LaunchFaster 启动器是本人近期编写的一款windows平台上快速启动应用的开源工具软件. LaunchFaster 启动器是一款类似于 Rolan 和 音速启动 和 Lily 的图标形式的 ...
- python学习之RabbitMQ-----消息队列
RabbitMQ队列 首先我们在讲rabbitMQ之前我们要说一下python里的queue:二者干的事情是一样的,都是队列,用于传递消息 在python的queue中有两个一个是线程queue,一个 ...
- 练手THINKPHP5过程和bootstrap3.3.7
1 在GIT上下载了最新版的源码,同时安装composer 用composer更新 git地址https://github.com/top-think/think 2 搭建本地开发环境,开启url重写 ...