题目链接:
力扣https://leetcode-cn.com/problems/sum-of-all-subset-xor-totals/
【分析】回溯法,用0,1表示这个是否在子集中。
class Solution {int[] arr, nums;int n, ans = 0;public void backTrack(int t){if(t == n){int a = arr[0] * nums[0];for(int i = 1; i < n; i++) a ^= arr[i] * nums[i];ans += a;return;}arr[t] = 1;backTrack(t + 1);arr[t] = 0;backTrack(t + 1);}public int subsetXORSum(int[] nums) {this.nums = nums;n = nums.length;arr = new int[n];if(n == 1) return nums[0];backTrack(0);return ans;}
}