Skip to content

Commit d529fb7

Browse files
committed
Add solution and test-cases for problem 1863
1 parent 09a1dcb commit d529fb7

File tree

3 files changed

+69
-22
lines changed

3 files changed

+69
-22
lines changed

leetcode/1801-1900/1863.Sum-of-All-Subset-XOR-Totals/README.md

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,53 @@
11
# [1863.Sum of All Subset XOR Totals][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
The **XOR total** of an array is defined as the bitwise XOR of **all its elements**, or `0` if the array is **empty**.
5+
6+
- For example, the **XOR total** of the array `[2,5,6]` is `2 XOR 5 XOR 6 = 1`.
7+
8+
Given an array `nums`, return the **sum** of all **XOR totals** for every **subset** of `nums`.
9+
10+
**Note**: Subsets with the **same** elements should be counted **multiple** times.
11+
12+
An array `a` is a **subset** of an array `b` if `a` can be obtained from `b` by deleting some (possibly zero) elements of `b`.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: nums = [1,3]
18+
Output: 6
19+
Explanation: The 4 subsets of [1,3] are:
20+
- The empty subset has an XOR total of 0.
21+
- [1] has an XOR total of 1.
22+
- [3] has an XOR total of 3.
23+
- [1,3] has an XOR total of 1 XOR 3 = 2.
24+
0 + 1 + 3 + 2 = 6
1325
```
1426

15-
## 题意
16-
> ...
27+
**Example 2:**
1728

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Sum of All Subset XOR Totals
23-
```go
2429
```
30+
Input: nums = [5,1,6]
31+
Output: 28
32+
Explanation: The 8 subsets of [5,1,6] are:
33+
- The empty subset has an XOR total of 0.
34+
- [5] has an XOR total of 5.
35+
- [1] has an XOR total of 1.
36+
- [6] has an XOR total of 6.
37+
- [5,1] has an XOR total of 5 XOR 1 = 4.
38+
- [5,6] has an XOR total of 5 XOR 6 = 3.
39+
- [1,6] has an XOR total of 1 XOR 6 = 7.
40+
- [5,1,6] has an XOR total of 5 XOR 1 XOR 6 = 2.
41+
0 + 5 + 1 + 6 + 4 + 3 + 7 + 2 = 28
42+
```
43+
44+
**Example 3:**
2545

46+
```
47+
Input: nums = [3,4,5,6,7,8]
48+
Output: 480
49+
Explanation: The sum of all XOR totals for every subset is 480.
50+
```
2651

2752
## 结语
2853

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
ans := 0
5+
for i := 0; i < len(nums); i++ {
6+
// l = 1
7+
ans += nums[i]
8+
}
9+
10+
var selector func(int, int, int)
11+
selector = func(index, l, cur int) {
12+
if l == 0 {
13+
ans += cur
14+
return
15+
}
16+
if index == len(nums) {
17+
return
18+
}
19+
20+
selector(index+1, l, cur)
21+
selector(index+1, l-1, cur^nums[index])
22+
}
23+
for l := 2; l <= len(nums); l++ {
24+
selector(0, l, 0)
25+
}
26+
return ans
527
}

leetcode/1801-1900/1863.Sum-of-All-Subset-XOR-Totals/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 3}, 6},
17+
{"TestCase2", []int{5, 1, 6}, 28},
18+
{"TestCase3", []int{3, 4, 5, 6, 7, 8}, 480},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)