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:
Given ["abcw", "baz", "foo", "bar", "xtfn", "abcdef"]
Return 16
The two words can be "abcw", "xtfn".
Example 2:
Given ["a", "ab", "abc", "d", "cd", "bcd", "abcd"]
Return 4
The two words can be "ab", "cd".
Example 3:
Given ["a", "aa", "aaa", "aaaa"]
Return 0
No such pair of words.
public static int maxProduct(String[] words){
int[] arri = new int[words.length];
for (int i = 0; i < words.length; i++) {
for (int j = 0; j < words[i].length(); j++) {
arri[i] |= 1<<(words[i].charAt(j)-'a');
}
}
int ans = 0;
for (int i = 0; i < words.length-1; i++) {
for (int j = i+1; j < words.length; j++) {
if ((arri[i] & arri[j]) == 0) {// 没有重复的字母
ans = Math.max(ans, words[i].length()*words[j].length());
}
}
}
return ans;
}
移位运算符:
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 【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 ——本质:英文单词中字符是否出现可以用26bit的整数表示
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- 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 ...
- [LC] 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 位运算 日期 题目地址:https://le ...
- 318 Maximum Product of Word Lengths 最大单词长度乘积
给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...
随机推荐
- CSS中的绝对定位与相对定位
层级关系为:<div ----------- position:relative; 不是最近的祖先定位元素,不是参照物<div----------没有设置为定位元素,不是参照物<di ...
- Cassandra 介绍
cassandra是一种NoSQL数据库,No是指No Relational.cassandra的数据模型结合了Dynamo的key/value和BigTable 的面向列的特点,主要被设计为存储大 ...
- Python基础(二)之模块
模块:人们写好的一系列用于实现某种功能的代码封装起来,需要使用的时候直接调用即可. 模块分类:标准模块.第三方模块 标准模块:不需要安装,直接调用即可 第三方模块:需要安装后才可使用 注意:自己创建的 ...
- mysql中的模糊查询
转载自:http://www.letuknowit.com/archives/90/ MySQL中实现模糊查询有2种方式:一是用LIKE/NOT LIKE,二是用REGEXP/NOT REGEXP(或 ...
- 定位form光标行
In AX2009 if you call the research() method on a form passing true, the cursor position within a gri ...
- Django 静态文件配置(static files)
Django version: 1.9 Python versrion: 3.5.2 这几天Django配置静态文件(本例是要加载index.css), 总是不对,最后终于试对了,这里记录下,方便以后 ...
- FreeBSD从零开始---安装后配置(二)
系统优化及安全设置 上次说了FreeBSD基本的软件安装和配置,接下来会说系统的详细配置和安全性设置 一.系统优化设置 1.网络相关设置 网卡和IP地址设置: 如果在安装时没有设置IP ...
- Android开发-Android Studio使用问题解决
回头一看,很久没来更新了,归其原因,还是懒癌发作,倒是生活作息规律了,几乎每天都在11点前休息.今天趁着培训,使用android studio,发现几个坑: 1.android studio每次都提示 ...
- java 将字符串下载为文本文件
通过url访问方法即可进行下载 @RequestMapping("down") public String down(HttpServletRequest request,Http ...
- spring资料
spring的官方文档还是很全面的: http://link.zhihu.com/?target=http%3A//docs.spring.io/spring/docs/current/spring- ...