Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions leetcode/2001-2100/2032.Two-Out-of-Three/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
# [2032.Two Out of Three][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
Given three integer arrays `nums1`, `nums2`, `nums3`, return a **distinct** array containing all the values that are present in **at least two** out of the three arrays. You may return the values in `any` order.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums1 = [1,1,3,2], nums2 = [2,3], nums3 = [3]
Output: [3,2]
Explanation: The values that are present in at least two arrays are:
- 3, in all three arrays.
- 2, in nums1 and nums2.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Two Out of Three
```go
```
Input: nums1 = [3,1], nums2 = [2,3], nums3 = [1,2]
Output: [2,3,1]
Explanation: The values that are present in at least two arrays are:
- 2, in nums2 and nums3.
- 3, in nums1 and nums2.
- 1, in nums1 and nums3.
```

**Example 3:**

```
Input: nums1 = [1,2,2], nums2 = [4,3,3], nums3 = [5]
Output: []
Explanation: No value is present in at least two arrays.
```

## 结语

Expand Down
22 changes: 20 additions & 2 deletions leetcode/2001-2100/2032.Two-Out-of-Three/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(nums1 []int, nums2 []int, nums3 []int) []int {
ans := make([]int, 0)
bucket := make([]int, 101)
for _, n := range nums1 {
bucket[n] |= 1
}
for _, n := range nums2 {
bucket[n] |= 2
}
for _, n := range nums3 {
bucket[n] |= 4
}

// 000, 001, 010, 011, 100, 101, 110,111
for idx, n := range bucket {
if n == 3 || n == 5 || n == 6 || n == 7 {
ans = append(ans, idx)
}
}
return ans
}
16 changes: 8 additions & 8 deletions leetcode/2001-2100/2032.Two-Out-of-Three/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
n1, n2, n3 []int
expect []int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 1, 3, 2}, []int{2, 3}, []int{3}, []int{2 ,3}},
{"TestCase2", []int{3, 1}, []int{2, 3}, []int{1, 2}, []int{1, 2, 3}},
{"TestCase3", []int{1, 2, 2}, []int{4, 3, 3}, []int{5}, []int{}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n1, c.n2, c.n3)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.n1, c.n2, c.n3)
}
})
}
Expand Down