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])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...
随机推荐
- 为没有源码的DLL文件添加强名称
为没有源码的DLL文件添加强名称 如果项目中引用了其他没有源码的dll文件,并且此dll文件是没有强名称的程序集,则编译时会出现类似 "Assembly generation failed ...
- 【转】STM32中的抢占优先级、响应优先级概念
STM32(Cortex-M3)中有两个优先级的概念--抢占式优先级和响应优先级,有人把响应优先级称作'亚优先级'或'副优先级',每个中断源都需要被指定这两种优先级. 具有高抢占式优先级的中断可以在具 ...
- [原]CentOS 6.5 上安装 MySQL 5.6
参考文档: http://dev.mysql.com/doc/refman/5.6/en/linux-installation-yum-repo.html 1. 下载 rpm 文件:wget http ...
- maven 项目无法发布,无法编译的解决办法
1 Web Deployment Assembly信息都合理2 重新clear项目,让JAVA代码重新生成.class文件在target目录中
- java对xml文件的读取
<?xml version="1.0" encoding="UTF-8"?> <body> <names type="1 ...
- gradle修改AndroidManifest.xml中的版本号
def VersionCode = "19" ant.replaceregexp(file:"../Assets/Plugins/Android/AndroidManif ...
- Laravel学习笔记(一)安装配置开发环境
摘要 Laravel的目标是给开发者创造一个愉快的开发过程,并且不牺牲应用的功能性.快乐的开发者才能创造最棒的代码!为了这个目的,开发者博取众框架之长处集中到Laravel中,这些框架甚至是基于Rub ...
- 模拟下载的进度条ProgressBar
作者:堕落的天使 图片效果 activity_main.xml(代码) <RelativeLayout xmlns:android="http://schemas.android.co ...
- ASP.net 中手工调用WS(POST方式)
ASP.net 中手工调用WS(POST方式)核心代码:string strUrl="http://localhost:21695/service1.asmx/getmythmod" ...
- http://www.cnblogs.com/nullcnb/p/3653262.html
alert(hasPrototypeProperty(person,"name")); //false 来自原型 person1.name="mywei"; a ...