[LeetCode]Maximum Product of Word Lengths(Java)
2017-01-06 14:58
423 查看
我自己用暴力解法发现不能通过,其中有一个测试用例使用的单个字符串非常长
我的代码如下
public class Solution {
public int maxProduct(String[] words) {
int max = 0;
Arrays.sort(words, new Comparator<String>(){
public int compare(String a, String b){
return b.length() - a.length();
}
});
for(int i = 0;i < words.length;i++){
for(int j = i + 1;j < words.length;j++){
if(!checkShare(words[i],words[j]) ){
max = Math.max(max,words[i].length() * words[j].length());
}
}
}
return max;
}
private boolean checkShare(String a,String b){
for(int i = 0;i < a.length();i++){
if(b.indexOf(""+a.charAt(i)) != -1)
return true;
}
return false;
}
}发现top solution 都是用未操作去存数的一共32位存26个字符完全充足
public static int maxProduct(String[] words) {
if (words == null || words.length == 0)
return 0;
int len = words.length;
int[] value = new int[len];
for (int i = 0; i < len; i++) {
String tmp = words[i];
value[i] = 0;
for (int j = 0; j < tmp.length(); j++) {
value[i] |= 1 << (tmp.charAt(j) - 'a');
}
}
int maxProduct = 0;
for (int i = 0; i < len; i++)
for (int j = i + 1; j < len; j++) {
if ((value[i] & value[j]) == 0 && (words[i].length() * words[j].length() > maxProduct))
maxProduct = words[i].length() * words[j].length();
}
return maxProduct;
}
在下佩服
2017/1/6
我的代码如下
public class Solution {
public int maxProduct(String[] words) {
int max = 0;
Arrays.sort(words, new Comparator<String>(){
public int compare(String a, String b){
return b.length() - a.length();
}
});
for(int i = 0;i < words.length;i++){
for(int j = i + 1;j < words.length;j++){
if(!checkShare(words[i],words[j]) ){
max = Math.max(max,words[i].length() * words[j].length());
}
}
}
return max;
}
private boolean checkShare(String a,String b){
for(int i = 0;i < a.length();i++){
if(b.indexOf(""+a.charAt(i)) != -1)
return true;
}
return false;
}
}发现top solution 都是用未操作去存数的一共32位存26个字符完全充足
public static int maxProduct(String[] words) {
if (words == null || words.length == 0)
return 0;
int len = words.length;
int[] value = new int[len];
for (int i = 0; i < len; i++) {
String tmp = words[i];
value[i] = 0;
for (int j = 0; j < tmp.length(); j++) {
value[i] |= 1 << (tmp.charAt(j) - 'a');
}
}
int maxProduct = 0;
for (int i = 0; i < len; i++)
for (int j = i + 1; j < len; j++) {
if ((value[i] & value[j]) == 0 && (words[i].length() * words[j].length() > maxProduct))
maxProduct = words[i].length() * words[j].length();
}
return maxProduct;
}
在下佩服
2017/1/6
相关文章推荐
- Java [Leetcode 318]Maximum Product of Word Lengths
- LeetCode 318 -Maximum Product of Word Lengths ( JAVA )
- [LeetCode 318] Maximum Product of Word Lengths
- LeetCode-Maximum Product of Word Lengths
- leetcode -- Maximum Product of Word Lengths -- 重点
- Leetcode:Missing Number & Maximum Product of Word Lengths
- LeetCode Maximum Product of Word Lengths
- [leetcode 318]Maximum Product of Word Lengths--判断两个字符串是否有相同的字符
- leetcode笔记:Maximum Product of Word Lengths
- [LeetCode][JavaScript]Maximum Product of Word Lengths
- [LeetCode] Maximum Product of Word Lengths 单词长度的最大积
- LeetCode318——Maximum Product of Word Lengths,从time limit exceeded到accept
- Leetcode 318 Maximum Product of Word Lengths 字符串处理+位运算
- LeetCode "Maximum Product of Word Lengths"
- LeetCode:Maximum Product of Word Lengths
- Maximum Product of Word Lengths -- LeetCode
- LeetCode Maximum Product of Word Lengths
- LeetCode: Maximum Product of Word Lengths
- Leetcode202: Maximum Product of Word Lengths
- [leetcode] Maximum Product of Word Lengths