思路如下:递归思路,依次遍历数组中的数,当前数要不要选择像二叉树一样去遍历如下图所示
0
0 (选5) 5(不选5)
0 1 0 1
0 6 0 6 0 6 0 6
0+6+1+7+5+3+4+2=28
class Solution {
public:int res = 0;int n = 0;int subsetXORSum(vector<int>& nums) {n = nums.size();dfs(nums,0,0);return res;}void dfs(vector<int>& nums,int val,int idx){//如果到数组尾部,则加上或与的和if(idx == n){res += val;return;}//选择当前节点dfs(nums,val^nums[idx],idx+1);//不选择当前节点dfs(nums,val,idx+1);}
};