public class Solution { public int MaxProduct(string[] words) { bool flag; int pro = 0; int N = words.Length; int[] char_exist; char_exist = new int[N]; Array.Clear(char_exist,0,N); for(int i=0;i<N;i++) { for(int k=0;k<words[i].Length;k++) char_exist[i] |= 1<<(words[i][k] - 'a'); } for(int i=0;i<N-1;i++) { for(int j=i+1;j<N;j++) { flag = true; if((char_exist[i] & char_exist[j]) !=0) { flag = false; } if(flag == true) pro = Math.Max((words[i].Length)*(words[j].Length),pro); if(pro== int.MaxValue)return pro; } } return pro; } }
思想 用一个int数组,记录每个word里面出现过哪些字符,两个int之间“与”运算,得知他们是否有共同元素
语言 c#