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;
}

移位运算符:

1 << 0 => 1
1 << 1 => 2
1 << 2 => 4
1 << 3 => 8
1 << 4 => 16
1 << 5 => 32
1 << 6 => 64
1 << 7 => 128
1 << 8 => 256
1 << 9 => 512
1 << 10 => 1024
1 << 11 => 2048
1 << 12 => 4096
1 << 13 => 8192
1 << 14 => 16384
1 << 15 => 32768
1 << 16 => 65536
1 << 17 => 131072
1 << 18 => 262144
1 << 19 => 524288
1 << 20 => 1048576
1 << 21 => 2097152
1 << 22 => 4194304
1 << 23 => 8388608
1 << 24 => 16777216
1 << 25 => 33554432
 
words[i] 对应着arri[i]:words[i]中的每个单词编码后得到arri[i]
注释处相交等于0说明有重复字母。
 
 

318. Maximum Product of Word Lengths的更多相关文章

  1. leetcode 318. Maximum Product of Word Lengths

    传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. [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 ...

  7. [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 ...

  8. 【LeetCode】318. Maximum Product of Word Lengths 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 set 位运算 日期 题目地址:https://le ...

  9. 318 Maximum Product of Word Lengths 最大单词长度乘积

    给定一个字符串数组words,找到length(word[i]) * length(word[j])的最大值,并且两个单词不含公共的字母.你可以认为每个单词只包含小写字母.如果不存在这样的两个单词,返 ...

随机推荐

  1. oracle 导库建立测试库

    由于客户要定制的关系,需要对产品的数据进行相关的修改,所以需要复制原来的库出来,然后在此基础上再进行修改.步骤如下: 在PL/SQL下操作: /*分为四步 *//*第1步:创建临时表空间  */cre ...

  2. Mysql的row_format

    在mysql中, 若一张表里面不存在varchar.text以及其变形.blob以及其变形的字段的话,那么张这个表其实也叫静态表,即该表的row_format是fixed,就是说每条记录所占用的字节一 ...

  3. php判断是否是微信客户端的浏览器访问

    代码: $user_agent = $_SERVER['HTTP_USER_AGENT']; if (strpos($user_agent, 'MicroMessenger') === false) ...

  4. 树莓派3B 安装微雪LCD5inch显示器(包含软键盘)

    树莓派单独使用时,往往需要触摸屏和软键盘用以方便操作,微雪LCD显示器就能较好的实现这个功能, 正好实验室又买入了一个3B的板子和一个5inch的显示器,便对着官方的安装手册,亲自安装了一次. 一:材 ...

  5. struts的标签库出现Failed to load or instantiate TagExtraInfo class

    使用struts的标签库出现Failed to load or instantiate TagExtraInfo class 最近在使用struts标签库的时候,在eclipse开发环境中是正常的,放 ...

  6. 前端设计师也有必要学习seo,推荐一个seo博客

    做前端设计师有一段时间了,现在越来越觉得作为一个前端设计师,必须要懂一些seo的知识. 因为公司的seo们,总是在网站做好以后,提出各种各样的网站修改的需求. 如果前端设计师,能够了解一些基本的seo ...

  7. Django中提示TemplateDoesNotExist?

    用的是1.9版本.需要在settings.py文件中设置TEMPLATES下的DIRS如下: TEMPLATES = [ { 'BACKEND': 'django.template.backends. ...

  8. IIS7下ajax报未定义错误

    项目之前在iis6环境下运行的很好,今天在WIN7下发布,结果居然报对象未定义错误,经过个把小时折腾,终于弄清楚原委. 在web.config中关于AjaxPro的设置,在IIS7.0(WIN7中使用 ...

  9. 交换机做Channel-Group

    core1#conf tEnter configuration commands, one per line.  End with CNTL/Z.core1(config)#inter range g ...

  10. 转@ManyToMany- annotation关系映射篇(下)

    原文:http://blog.sina.com.cn/s/blog_6fef491d0100obdd.html 终于要说ManyToMany了 场景:Product和Customer. 先看TestP ...