public static long section (int[] a, int t) { if(t==0) return 0; return subSection(a,t,0,a.length-1); } public static long subSection(int[] nums,int t,int left,int right){ if(left>right) return 0; if(left==right) return 0; HashMap<Integer,Integer> valueIndexMap =new HashMap<>(); for (int i = left; i <= right; i++) { if(valueIndexMap.containsKey(nums[i])){ int index0 = valueIndexMap.get(nums[i]); int index1 = i; return subSection(nums,t,left,index1-1)+subSection(nums,t,index0+1,right); } valueIndexMap.put(t^nums[i],i); } //没有找到任何亦或 long size = right-left+1; //(k-1)+...+2+1 = k(k-1)/2 return size*(size-1)/2; }
全部评论
(1) 回帖