forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1863.java
36 lines (32 loc) · 1.07 KB
/
_1863.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _1863 {
public static class Solution1 {
public int subsetXORSum(int[] nums) {
int sum = 0;
List<List<Integer>> subsets = subsets(nums);
for (List<Integer> subset : subsets) {
int xor = 0;
for (int i : subset) {
xor ^= i;
}
sum += xor;
}
return sum;
}
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList();
backtracking(result, new ArrayList(), nums, 0);
return result;
}
void backtracking(List<List<Integer>> result, List<Integer> list, int[] nums, int start) {
result.add(new ArrayList(list));
for (int i = start; i < nums.length; i++) {
list.add(nums[i]);
backtracking(result, list, nums, i + 1);
list.remove(list.size() - 1);
}
}
}
}