318. Maximum Product of Word Lengths

来源:互联网 发布:nginx php 空白 编辑:程序博客网 时间:2024/06/01 19:26

题目:https://leetcode.com/problems/maximum-product-of-word-lengths/

代码:

思路:    用marks数组来区分两个字符串是否有相同的字母。在本程序中每个字母对应一位的1,比如a对应1,b对应10(二进制),c对应100.。。。。。。那么如果marks与一下是0的话说明没有相同的字母public class Solution {    public int maxProduct(String[] words) {        int[] marks = new int[words.length];        for(int i=0;i<marks.length;i++)        {            char[] temp = words[i].toCharArray();            for(int j=0;j<temp.length;j++)            {                int p = (int)(temp[j]-'a');                p = 1<<p;                marks[i] |= p;            }        }        int max = 0;        for(int i=0;i<marks.length;i++)        {            for(int j=i+1;j<marks.length;j++)            {                if((marks[i]&marks[j])==0)                    max = Math.max(max,words[i].length()*words[j].length());            }        }        return max;    }}33ms
0 0
原创粉丝点击