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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 33 additions & 28 deletions leetcode/1101-1200/1115.Print-FooBar-Alternately/README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,53 @@
# [1. Add Sum][title]
# [1115. Print FooBar Alternately][title]

## Description

Given two binary strings, return their sum (also a binary string).

The input strings are both **non-empty** and contains only characters `1` or `0`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```

**Example 2:**

```
Input: a = "1010", b = "1011"
Output: "10101"
Suppose you are given the following code:

```cpp
class FooBar {
public void foo() {
for (int i = 0; i < n; i++) {
print("foo");
}
}

public void bar() {
for (int i = 0; i < n; i++) {
print("bar");
}
}
}
```

**Tags:** Math, String
The same instance of `FooBar` will be passed to two different threads:

## 题意
> 求2数之和
- thread `A` will call `foo()`, while
- thread `B` will call `bar()`.

## 题解
Modify the given program to output "`foobar`" `n` times.

### 思路1
> 。。。。

```go
**Example 1:**

```
Input: n = 1
Output: "foobar"
Explanation: There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar().
"foobar" is being output 1 time.
```

### 思路2
> 思路2
```go
**Example 2:**

```
Input: n = 2
Output: "foobarfoobar"
Explanation: "foobar" is being output 2 times.
```

## 结语

如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]

[title]: https://leetcode.com/problems/two-sum/description/
[title]: https://leetcode.com/problems/print-foobar-alternately
[me]: https://github.com/kylesliu/awesome-golang-algorithm
29 changes: 25 additions & 4 deletions leetcode/1101-1200/1115.Print-FooBar-Alternately/Solution.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
package Solution

func Solution_1(x bool) bool {
return x
type FooBar struct {
n int
a, b chan struct{}
}

func Solution_2(x bool) bool {
return x
func Solution(n int) *FooBar {
f := &FooBar{n: n, a: make(chan struct{}, 1), b: make(chan struct{}, 1)}
f.b <- struct{}{}
return f
}

func (fb *FooBar) Foo(printFoo func()) {
for i := 0; i < fb.n; i++ {
// printFoo() outputs "foo". Do not change or remove this line.
<-fb.b
printFoo()
fb.a <- struct{}{}
}
}

func (fb *FooBar) Bar(printBar func()) {
for i := 0; i < fb.n; i++ {
// printBar() outputs "bar". Do not change or remove this line.
<-fb.a
printBar()
fb.b <- struct{}{}
}
}
83 changes: 52 additions & 31 deletions leetcode/1101-1200/1115.Print-FooBar-Alternately/Solution_test.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,69 @@
package Solution

import (
"bytes"
"fmt"
"io"
"reflect"
"runtime"
"strings"
"strconv"
"sync"
"testing"

"github.com/stretchr/testify/assert"
)

// Solution func Info
type SolutionFuncType func(x bool) bool

var SolutionFuncList = []SolutionFuncType{
Solution_1,
Solution_2,
func foo(output io.Writer) func() {
return func() {
fmt.Fprint(output, "foo")
}
}

// Test case info struct
type Case struct {
name string
input bool
expect bool
func bar(output io.Writer) func() {
return func() {
fmt.Fprint(output, "bar")
}
}

// Test case
var cases = []Case{
{name: "TestCase 1", input: true, expect: true},
{name: "TestCase 2", input: false, expect: false},
func run(fb *FooBar) string {
var wg sync.WaitGroup
wg.Add(2)
buf := bytes.NewBuffer([]byte{})
go func() {
defer wg.Done()
fb.Foo(foo(buf))
}()
go func() {
defer wg.Done()
fb.Bar(bar(buf))
}()
wg.Wait()
return buf.String()
}

// TestSolution Run test case for all solutions
func TestSolution(t *testing.T) {
ast := assert.New(t)
// 测试用例
cases := []struct {
name string
inputs int
expect string
}{
{"TestCase1", 1, "foobar"},
{"TestCase2", 2, "foobarfoobar"},
}

for _, f := range SolutionFuncList {
funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), ".")[1]
for _, c := range cases {
t.Run(fmt.Sprintf("%s %s", funcName, c.name), func(t *testing.T) {
got := f(c.input)
ast.Equal(c.expect, got,
"func: %v case: %v ", funcName, c.name)
})
}
// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := run(Solution(c.inputs))
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
}
})
}
}

// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
func ExampleSolution() {
}
Loading