-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat: add solution for problem no.1114 and no.1115 #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
*.toml | ||
.idea | ||
.vscode |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package leetcode | ||
|
||
import "fmt" | ||
|
||
type Foo struct { | ||
chans []chan bool | ||
} | ||
|
||
func NewFoo() *Foo { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这道题官方没有 go 的语言选择。你那边可以用 go 提交代码么? |
||
pt := Foo{} | ||
pt.chans = make([]chan bool, 0) | ||
for i := 0; i < 2; i++ { | ||
pt.chans = append(pt.chans, make(chan bool, 1)) | ||
} | ||
return &pt | ||
} | ||
|
||
func (this *Foo) first() { | ||
fmt.Print("first") | ||
this.chans[0] <- true | ||
} | ||
|
||
func (this *Foo) second() { | ||
select { | ||
case <-this.chans[0]: | ||
fmt.Print("second") | ||
this.chans[1] <- true | ||
} | ||
} | ||
|
||
func (this *Foo) third() { | ||
select { | ||
case <-this.chans[1]: | ||
fmt.Print("third") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package leetcode | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type question1114 struct { | ||
para1114 | ||
ans1114 | ||
} | ||
|
||
// para 是参数 | ||
// one 代表第一个参数 | ||
type para1114 struct { | ||
one []int | ||
} | ||
|
||
// ans 是答案 | ||
// one 代表第一个答案 | ||
type ans1114 struct { | ||
one string | ||
} | ||
|
||
func Test_Problem1114(t *testing.T) { | ||
|
||
qs := []question1114{ | ||
|
||
{ | ||
para1114{[]int{1, 2, 3}}, | ||
ans1114{"firstsecondthird"}, | ||
}, | ||
|
||
{ | ||
para1114{[]int{1, 3, 2}}, | ||
ans1114{"firstsecondthird"}, | ||
}, | ||
|
||
{ | ||
para1114{[]int{2, 1, 3}}, | ||
ans1114{"firstsecondthird"}, | ||
}, | ||
|
||
{ | ||
para1114{[]int{2, 3, 1}}, | ||
ans1114{"firstsecondthird"}, | ||
}, | ||
|
||
{ | ||
para1114{[]int{3, 1, 2}}, | ||
ans1114{"firstsecondthird"}, | ||
}, | ||
|
||
{ | ||
para1114{[]int{3, 2, 1}}, | ||
ans1114{"firstsecondthird"}, | ||
}, | ||
} | ||
|
||
fmt.Printf("------------------------Leetcode Problem 1114------------------------\n") | ||
|
||
for _, q := range qs { | ||
_, p := q.ans1114, q.para1114 | ||
fooIns := NewFoo() | ||
fmt.Printf("【input】:%v 【output】:", p) | ||
for _, v := range q.para1114.one { | ||
switch v { | ||
case 1: | ||
go fooIns.first() | ||
case 2: | ||
go fooIns.second() | ||
case 3: | ||
go fooIns.third() | ||
default: | ||
panic("problem does not support input other than '1, 2, 3'") | ||
} | ||
} | ||
time.Sleep(time.Second) | ||
fmt.Printf("\n") | ||
} | ||
fmt.Printf("\n\n\n") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# [1114. Print in Order](https://leetcode.com/problems/print-in-order/) | ||
|
||
## 题目 | ||
|
||
Suppose we have a class: | ||
|
||
```java | ||
public class Foo { | ||
public void first() { print("first"); } | ||
public void second() { print("second"); } | ||
public void third() { print("third"); } | ||
} | ||
``` | ||
|
||
The same instance of Foo will be passed to three different threads. Thread A will call `first()`, thread B will call `second()`, and thread C will call `third()`. Design a mechanism and modify the program to ensure that `second()` is executed after `first()`, and `third()` is executed after `second()`. | ||
|
||
**Note:** | ||
|
||
We do not know how the threads will be scheduled in the operating system, even though the numbers in the input seem to imply the ordering. The input format you see is mainly to ensure our tests' comprehensiveness. | ||
|
||
**Example 1:** | ||
|
||
Input: nums = [1,2,3] | ||
Output: "firstsecondthird" | ||
Explanation: There are three threads being fired asynchronously. The input [1,2,3] means thread A calls first(), thread B calls second(), and thread C calls third(). "firstsecondthird" is the correct output. | ||
|
||
**Example 2:** | ||
|
||
Input: nums = [1,3,2] | ||
Output: "firstsecondthird" | ||
Explanation: The input [1,3,2] means thread A calls first(), thread B calls third(), and thread C calls second(). "firstsecondthird" is the correct output. | ||
|
||
**Constraints:** | ||
|
||
- `nums` is a permutation of `[1, 2, 3]`. | ||
|
||
|
||
## 题目大意 | ||
|
||
实现一个可以按需打印“first”、“second”和“third”的类,无论并发调用的顺序如何,均可保持按从小到大打印的位次。 | ||
|
||
## 解题思路 | ||
|
||
- Golang中可以使用Channel实现多个协程执行的顺序依赖。通过有缓冲区的Channel实现写入不阻塞,读取阻塞的功能;通过select使预期靠后执行的协程阻塞等待其它协程执行完毕。 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package leetcode | ||
|
||
import "fmt" | ||
|
||
type FooBar struct { | ||
chans []chan bool | ||
limit int | ||
printed []int | ||
} | ||
|
||
func NewFooBar(n int) *FooBar { | ||
pt := FooBar{} | ||
pt.limit = n | ||
pt.printed = make([]int, 2) | ||
pt.chans = make([]chan bool, 2) | ||
for idx := range pt.chans { | ||
pt.chans[idx] = make(chan bool, 1) | ||
} | ||
pt.chans[0] <- true | ||
return &pt | ||
} | ||
|
||
func (this *FooBar) foo() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 函数名错了,要大写。并且入参需要输入一个函数:
|
||
for this.printed[0] < this.limit { | ||
select { | ||
case <-this.chans[0]: | ||
if this.printed[0] >= this.limit { | ||
return | ||
} | ||
fmt.Printf("foo") | ||
this.printed[0]++ | ||
this.chans[1] <- true | ||
} | ||
} | ||
} | ||
|
||
func (this *FooBar) bar() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 函数名错了,要大写。并且入参需要输入一个函数:
|
||
for this.printed[1] < this.limit { | ||
select { | ||
case <-this.chans[1]: | ||
if this.printed[1] >= this.limit { | ||
return | ||
} | ||
fmt.Printf("bar") | ||
this.printed[1]++ | ||
this.chans[0] <- true | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package leetcode | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
"time" | ||
) | ||
|
||
type question1115 struct { | ||
para1115 | ||
ans1115 | ||
} | ||
|
||
// para 是参数 | ||
// one 代表第一个参数 | ||
type para1115 struct { | ||
one int | ||
} | ||
|
||
// ans 是答案 | ||
// one 代表第一个答案 | ||
type ans1115 struct { | ||
one string | ||
} | ||
|
||
func Test_Problem1115(t *testing.T) { | ||
|
||
qs := []question1115{ | ||
|
||
{ | ||
para1115{1}, | ||
ans1115{"foobar"}, | ||
}, | ||
|
||
{ | ||
para1115{2}, | ||
ans1115{"foobarfoobar"}, | ||
}, | ||
|
||
{ | ||
para1115{3}, | ||
ans1115{"foobarfoobarfoobar"}, | ||
}, | ||
|
||
{ | ||
para1115{4}, | ||
ans1115{"foobarfoobarfoobarfoobar"}, | ||
}, | ||
|
||
{ | ||
para1115{5}, | ||
ans1115{"foobarfoobarfoobarfoobarfoobar"}, | ||
}, | ||
|
||
{ | ||
para1115{6}, | ||
ans1115{"foobarfoobarfoobarfoobarfoobarfoobar"}, | ||
}, | ||
} | ||
|
||
fmt.Printf("------------------------Leetcode Problem 1115------------------------\n") | ||
|
||
for _, q := range qs { | ||
fooBarIns := NewFooBar(q.para1115.one) | ||
fmt.Printf("【input】:%v 【output】:", q.para1115.one) | ||
coin := rand.Intn(1) | ||
if coin == 0 { | ||
go fooBarIns.foo() | ||
go fooBarIns.bar() | ||
} else { | ||
go fooBarIns.bar() | ||
go fooBarIns.foo() | ||
} | ||
time.Sleep(time.Second) | ||
fmt.Printf("\n") | ||
} | ||
fmt.Printf("\n\n\n") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这部分应该不用改动