Skip to content

Commit b7d07bc

Browse files
committed
feat: add solutions to lc problems: No.1114,1115
* No.1114.Print in Order * No.1115.Print FooBar Alternately
1 parent 7be2ed1 commit b7d07bc

File tree

10 files changed

+371
-68
lines changed

10 files changed

+371
-68
lines changed

solution/1100-1199/1114.Print in Order/README.md

+75-1
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,96 @@ public class Foo {
6565

6666
<!-- 这里可写通用的实现逻辑 -->
6767

68+
用两个锁或信号量分别锁住 `second``third`,在 `first` 执行完后释放 `second` 的锁,`second` 执行完后释放 `third` 的锁
69+
6870
<!-- tabs:start -->
6971

7072
### **Python3**
7173

7274
<!-- 这里可写当前语言的特殊实现逻辑 -->
7375

7476
```python
75-
77+
class Foo:
78+
def __init__(self):
79+
self.l2 = threading.Lock()
80+
self.l3 = threading.Lock()
81+
self.l2.acquire()
82+
self.l3.acquire()
83+
84+
def first(self, printFirst: 'Callable[[], None]') -> None:
85+
printFirst()
86+
self.l2.release()
87+
88+
def second(self, printSecond: 'Callable[[], None]') -> None:
89+
self.l2.acquire()
90+
printSecond()
91+
self.l3.release()
92+
93+
def third(self, printThird: 'Callable[[], None]') -> None:
94+
self.l3.acquire()
95+
printThird()
7696
```
7797

7898
### **Java**
7999

80100
<!-- 这里可写当前语言的特殊实现逻辑 -->
81101

82102
```java
103+
class Foo {
104+
105+
private final Semaphore s2 = new Semaphore(0);
106+
private final Semaphore s3 = new Semaphore(0);
107+
108+
public Foo() {
109+
}
110+
111+
public void first(Runnable printFirst) throws InterruptedException {
112+
printFirst.run();
113+
s2.release();
114+
}
115+
116+
public void second(Runnable printSecond) throws InterruptedException {
117+
s2.acquire();
118+
printSecond.run();
119+
s3.release();
120+
}
121+
122+
public void third(Runnable printThird) throws InterruptedException {
123+
s3.acquire();
124+
printThird.run();
125+
}
126+
}
127+
```
83128

129+
### **C++**
130+
131+
```cpp
132+
class Foo {
133+
private:
134+
mutex m2, m3;
135+
136+
public:
137+
Foo() {
138+
m2.lock();
139+
m3.lock();
140+
}
141+
142+
void first(function<void()> printFirst) {
143+
printFirst();
144+
m2.unlock();
145+
}
146+
147+
void second(function<void()> printSecond) {
148+
m2.lock();
149+
printSecond();
150+
m3.unlock();
151+
}
152+
153+
void third(function<void()> printThird) {
154+
m3.lock();
155+
printThird();
156+
}
157+
};
84158
```
85159
86160
### **...**

solution/1100-1199/1114.Print in Order/README_EN.md

+73-1
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,85 @@ public class Foo {
5151
### **Python3**
5252

5353
```python
54-
54+
class Foo:
55+
def __init__(self):
56+
self.l2 = threading.Lock()
57+
self.l3 = threading.Lock()
58+
self.l2.acquire()
59+
self.l3.acquire()
60+
61+
def first(self, printFirst: 'Callable[[], None]') -> None:
62+
printFirst()
63+
self.l2.release()
64+
65+
def second(self, printSecond: 'Callable[[], None]') -> None:
66+
self.l2.acquire()
67+
printSecond()
68+
self.l3.release()
69+
70+
def third(self, printThird: 'Callable[[], None]') -> None:
71+
self.l3.acquire()
72+
printThird()
5573
```
5674

5775
### **Java**
5876

5977
```java
78+
class Foo {
79+
80+
private final Semaphore s2 = new Semaphore(0);
81+
private final Semaphore s3 = new Semaphore(0);
82+
83+
public Foo() {
84+
}
85+
86+
public void first(Runnable printFirst) throws InterruptedException {
87+
printFirst.run();
88+
s2.release();
89+
}
90+
91+
public void second(Runnable printSecond) throws InterruptedException {
92+
s2.acquire();
93+
printSecond.run();
94+
s3.release();
95+
}
96+
97+
public void third(Runnable printThird) throws InterruptedException {
98+
s3.acquire();
99+
printThird.run();
100+
}
101+
}
102+
```
60103

104+
### **C++**
105+
106+
```cpp
107+
class Foo {
108+
private:
109+
mutex m2, m3;
110+
111+
public:
112+
Foo() {
113+
m2.lock();
114+
m3.lock();
115+
}
116+
117+
void first(function<void()> printFirst) {
118+
printFirst();
119+
m2.unlock();
120+
}
121+
122+
void second(function<void()> printSecond) {
123+
m2.lock();
124+
printSecond();
125+
m3.unlock();
126+
}
127+
128+
void third(function<void()> printThird) {
129+
m3.lock();
130+
printThird();
131+
}
132+
};
61133
```
62134
63135
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
11
class Foo {
2+
private:
3+
mutex m2, m3;
4+
25
public:
36
Foo() {
4-
_mutex1.lock();
5-
_mutex2.lock();
7+
m2.lock();
8+
m3.lock();
69
}
710

811
void first(function<void()> printFirst) {
9-
1012
printFirst();
11-
_mutex1.unlock();
13+
m2.unlock();
1214
}
1315

1416
void second(function<void()> printSecond) {
15-
16-
_mutex1.lock();
17+
m2.lock();
1718
printSecond();
18-
_mutex1.unlock();
19-
_mutex2.unlock();
19+
m3.unlock();
2020
}
2121

2222
void third(function<void()> printThird) {
23-
24-
_mutex2.lock();
23+
m3.lock();
2524
printThird();
26-
_mutex2.unlock();
2725
}
28-
29-
30-
private:
31-
std::mutex _mutex1;
32-
std::mutex _mutex2;
33-
3426
};
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
import java.util.concurrent.Semaphore;
2-
31
class Foo {
4-
private Semaphore twoS = new Semaphore(0);
5-
private Semaphore threeS = new Semaphore(0);
6-
2+
3+
private final Semaphore s2 = new Semaphore(0);
4+
private final Semaphore s3 = new Semaphore(0);
5+
76
public Foo() {
8-
97
}
108

119
public void first(Runnable printFirst) throws InterruptedException {
1210
printFirst.run();
13-
twoS.release();
11+
s2.release();
1412
}
1513

1614
public void second(Runnable printSecond) throws InterruptedException {
17-
twoS.acquire();
15+
s2.acquire();
1816
printSecond.run();
19-
threeS.release();
17+
s3.release();
2018
}
2119

2220
public void third(Runnable printThird) throws InterruptedException {
23-
threeS.acquire();
21+
s3.acquire();
2422
printThird.run();
2523
}
26-
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Foo:
2+
def __init__(self):
3+
self.l2 = threading.Lock()
4+
self.l3 = threading.Lock()
5+
self.l2.acquire()
6+
self.l3.acquire()
7+
8+
def first(self, printFirst: 'Callable[[], None]') -> None:
9+
printFirst()
10+
self.l2.release()
11+
12+
def second(self, printSecond: 'Callable[[], None]') -> None:
13+
self.l2.acquire()
14+
printSecond()
15+
self.l3.release()
16+
17+
def third(self, printThird: 'Callable[[], None]') -> None:
18+
self.l3.acquire()
19+
printThird()

solution/1100-1199/1115.Print FooBar Alternately/README.md

+76-1
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,97 @@ class FooBar {
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66+
两把锁分别对应 `foo``bar`,先把 `bar` 锁住,确保第一个输出的是 `foo`
67+
6668
<!-- tabs:start -->
6769

6870
### **Python3**
6971

7072
<!-- 这里可写当前语言的特殊实现逻辑 -->
7173

7274
```python
73-
75+
class FooBar:
76+
def __init__(self, n):
77+
self.n = n
78+
self.fooLock = threading.Lock()
79+
self.barLock = threading.Lock()
80+
self.barLock.acquire()
81+
82+
def foo(self, printFoo: 'Callable[[], None]') -> None:
83+
for i in range(self.n):
84+
self.fooLock.acquire()
85+
printFoo()
86+
self.barLock.release()
87+
88+
def bar(self, printBar: 'Callable[[], None]') -> None:
89+
for i in range(self.n):
90+
self.barLock.acquire()
91+
printBar()
92+
self.fooLock.release()
7493
```
7594

7695
### **Java**
7796

7897
<!-- 这里可写当前语言的特殊实现逻辑 -->
7998

8099
```java
100+
class FooBar {
101+
private int n;
102+
private final Semaphore fooSem = new Semaphore(1);
103+
private final Semaphore barSem = new Semaphore(0);
104+
105+
public FooBar(int n) {
106+
this.n = n;
107+
}
108+
109+
public void foo(Runnable printFoo) throws InterruptedException {
110+
for (int i = 0; i < n; i++) {
111+
fooSem.acquire();
112+
printFoo.run();
113+
barSem.release();
114+
}
115+
}
116+
117+
public void bar(Runnable printBar) throws InterruptedException {
118+
for (int i = 0; i < n; i++) {
119+
barSem.acquire();
120+
printBar.run();
121+
fooSem.release();
122+
}
123+
}
124+
}
125+
```
126+
127+
### **C++**
81128

129+
```cpp
130+
class FooBar {
131+
private:
132+
int n;
133+
mutex fooMu, barMu;
134+
135+
public:
136+
FooBar(int n) {
137+
this->n = n;
138+
barMu.lock();
139+
}
140+
141+
void foo(function<void()> printFoo) {
142+
for (int i = 0; i < n; i++) {
143+
fooMu.lock();
144+
printFoo();
145+
barMu.unlock();
146+
}
147+
}
148+
149+
void bar(function<void()> printBar) {
150+
for (int i = 0; i < n; i++) {
151+
barMu.lock();
152+
printBar();
153+
fooMu.unlock();
154+
}
155+
}
156+
};
82157
```
83158
84159
### **...**

0 commit comments

Comments
 (0)