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. 通过宏判断VS编译版本以及系统平台

    MSC_VER 定义编译器的版本.下面是一些编译器版本的_MSC_VER值(参见扩展阅读中的参考文献2的链接) MSVC++ 12.0 _MSC_VER == 1800 (Visual Studio ...

  2. CentOS 7如何连接无线网络

    虽然查阅了相关网络资料,但是以下内容均为原创内容,只有干货,无废话. 1.切换到超级用户 [Oscar@localhost 桌面]$ su root 2.查询可用的无线网卡,其中红色为网卡号 [Osc ...

  3. Kafka集群配置说明

    #kafka数据的存放地址,多个地址的话用逗号分 log.dirs=/tmp/kafka-logs #broker server服务端口 port=9092 #这个参数会在日志segment没有达到l ...

  4. SQLite常用语句

    // 错误存储路径 - (NSString *)dataFilePath{ NSString *dbPath = [NSSearchPathForDirectoriesInDomains(NSLibr ...

  5. 安卓 JDK、SDK、ADT 区别

    问题一:android软件开发是用java语法,但是为什么开发环境还需要jdk,有android sdk不就可以了吗? 答: 我知道写字要用笔,但为什么还需要笔芯(墨水),有笔杆不就可以了吗? 问题二 ...

  6. [源码]String StringBuffer StringBudlider(2)StringBuffer StringBuilder源码分析

      纵骑横飞 章仕烜   昨天比较忙 今天把StringBuffer StringBulider的源码分析 献上   在讲 StringBuffer StringBuilder 之前 ,我们先看一下 ...

  7. ThinkPHP 关于namespace的事儿

    如题,php通常是不允许函数重名的,例如a.php中有一个getName(),b.php中有一个getName(),在require_once a.php和b.php后就会报getName重复定义的错 ...

  8. 问题解决_(转载)VS2015无法启动 IIS Express Web解决办法

    将“重写应用程序根URL”的勾选去掉,然后就可正常运行 参考资料:http://www.qi88.com/edu/os/2015/0103/87648.html

  9. PHP中对淘宝URL中ID提取

    <?php $taobao = 'taobao.com'; $tmall = 'tmall.com'; $guojitmall = 'tmall.hk'; $juhuasuan = 'ju.ta ...

  10. Visual Studio 2012 trial version

    Update: vs2012.5.iso http://download.microsoft.com/download/9/F/1/9F1DEA0F-97CC-4CC4-9B4D-0DB45B8261 ...