Skip to content

Commit 7fa0e39

Browse files
authoredSep 6, 2023
feat: add solutions to lc problems: No.1207,1214 (doocs#1579)
* No.1207.Unique Number of Occurrences * No.1214.Two Sum BSTs
1 parent bd1d2e7 commit 7fa0e39

File tree

13 files changed

+564
-188
lines changed

13 files changed

+564
-188
lines changed
 

‎.github/workflows/compress.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
id: calibre
3535
uses: calibreapp/image-actions@main
3636
with:
37-
githubToken: ${{ secrets.ACTION_TOKEN }}
37+
githubToken: ${{ secrets.GITHUB_TOKEN }}
3838
# For non-Pull Requests, run in compressOnly mode and we'll PR after.
3939
compressOnly: ${{ github.event_name != 'pull_request' }}
4040
- name: Create Pull Request
@@ -44,7 +44,7 @@ jobs:
4444
steps.calibre.outputs.markdown != ''
4545
uses: peter-evans/create-pull-request@v4
4646
with:
47-
title: Auto Compress Images
47+
title: 'chore: auto compress images'
4848
branch-suffix: timestamp
49-
commit-message: Compress Images
49+
commit-message: 'chore: auto compress images'
5050
body: ${{ steps.calibre.outputs.markdown }}

‎solution/1100-1199/1195.Fizz Buzz Multithreaded/README.md

+59
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,66 @@ class FizzBuzz {
6565
<!-- 这里可写当前语言的特殊实现逻辑 -->
6666

6767
```java
68+
class FizzBuzz {
69+
private int n;
70+
71+
public FizzBuzz(int n) {
72+
this.n = n;
73+
}
6874

75+
private Semaphore fSema = new Semaphore(0);
76+
private Semaphore bSema = new Semaphore(0);
77+
private Semaphore fbSema = new Semaphore(0);
78+
private Semaphore nSema = new Semaphore(1);
79+
80+
// printFizz.run() outputs "fizz".
81+
public void fizz(Runnable printFizz) throws InterruptedException {
82+
for (int i = 3; i <= n; i = i + 3) {
83+
if (i % 5 != 0) {
84+
fSema.acquire();
85+
printFizz.run();
86+
nSema.release();
87+
}
88+
}
89+
}
90+
91+
// printBuzz.run() outputs "buzz".
92+
public void buzz(Runnable printBuzz) throws InterruptedException {
93+
for (int i = 5; i <= n; i = i + 5) {
94+
if (i % 3 != 0) {
95+
bSema.acquire();
96+
printBuzz.run();
97+
nSema.release();
98+
}
99+
}
100+
}
101+
102+
// printFizzBuzz.run() outputs "fizzbuzz".
103+
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
104+
for (int i = 15; i <= n; i = i + 15) {
105+
fbSema.acquire();
106+
printFizzBuzz.run();
107+
nSema.release();
108+
}
109+
}
110+
111+
// printNumber.accept(x) outputs "x", where x is an integer.
112+
public void number(IntConsumer printNumber) throws InterruptedException {
113+
for (int i = 1; i <= n; i++) {
114+
nSema.acquire();
115+
if (i % 3 == 0 && i % 5 == 0) {
116+
fbSema.release();
117+
} else if (i % 3 == 0) {
118+
fSema.release();
119+
} else if (i % 5 == 0) {
120+
bSema.release();
121+
} else {
122+
printNumber.accept(i);
123+
nSema.release();
124+
}
125+
}
126+
}
127+
}
69128
```
70129

71130
### **C++**

‎solution/1100-1199/1195.Fizz Buzz Multithreaded/README_EN.md

+120
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,127 @@
6969
### **Java**
7070

7171
```java
72+
class FizzBuzz {
73+
private int n;
74+
75+
public FizzBuzz(int n) {
76+
this.n = n;
77+
}
78+
79+
private Semaphore fSema = new Semaphore(0);
80+
private Semaphore bSema = new Semaphore(0);
81+
private Semaphore fbSema = new Semaphore(0);
82+
private Semaphore nSema = new Semaphore(1);
83+
84+
// printFizz.run() outputs "fizz".
85+
public void fizz(Runnable printFizz) throws InterruptedException {
86+
for (int i = 3; i <= n; i = i + 3) {
87+
if (i % 5 != 0) {
88+
fSema.acquire();
89+
printFizz.run();
90+
nSema.release();
91+
}
92+
}
93+
}
94+
95+
// printBuzz.run() outputs "buzz".
96+
public void buzz(Runnable printBuzz) throws InterruptedException {
97+
for (int i = 5; i <= n; i = i + 5) {
98+
if (i % 3 != 0) {
99+
bSema.acquire();
100+
printBuzz.run();
101+
nSema.release();
102+
}
103+
}
104+
}
105+
106+
// printFizzBuzz.run() outputs "fizzbuzz".
107+
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
108+
for (int i = 15; i <= n; i = i + 15) {
109+
fbSema.acquire();
110+
printFizzBuzz.run();
111+
nSema.release();
112+
}
113+
}
114+
115+
// printNumber.accept(x) outputs "x", where x is an integer.
116+
public void number(IntConsumer printNumber) throws InterruptedException {
117+
for (int i = 1; i <= n; i++) {
118+
nSema.acquire();
119+
if (i % 3 == 0 && i % 5 == 0) {
120+
fbSema.release();
121+
} else if (i % 3 == 0) {
122+
fSema.release();
123+
} else if (i % 5 == 0) {
124+
bSema.release();
125+
} else {
126+
printNumber.accept(i);
127+
nSema.release();
128+
}
129+
}
130+
}
131+
}
132+
```
72133

134+
### **C++**
135+
136+
```cpp
137+
class FizzBuzz {
138+
private:
139+
std::mutex mtx;
140+
atomic<int> index;
141+
int n;
142+
143+
public:
144+
FizzBuzz(int n) {
145+
this->n = n;
146+
index = 1;
147+
}
148+
149+
// printFizz() outputs "fizz".
150+
void fizz(function<void()> printFizz) {
151+
while (index <= n) {
152+
std::lock_guard<std::mutex> lk(mtx);
153+
if (0 == index % 3 && 0 != index % 5 && index <= n) {
154+
printFizz();
155+
index++;
156+
}
157+
}
158+
}
159+
160+
// printBuzz() outputs "buzz".
161+
void buzz(function<void()> printBuzz) {
162+
while (index <= n) {
163+
std::lock_guard<std::mutex> lk(mtx);
164+
if (0 == index % 5 && 0 != index % 3 && index <= n) {
165+
printBuzz();
166+
index++;
167+
}
168+
}
169+
}
170+
171+
// printFizzBuzz() outputs "fizzbuzz".
172+
void fizzbuzz(function<void()> printFizzBuzz) {
173+
while (index <= n) {
174+
std::lock_guard<std::mutex> lk(mtx);
175+
if (0 == index % 15 && index <= n) {
176+
printFizzBuzz();
177+
index++;
178+
}
179+
}
180+
}
181+
182+
// printNumber(x) outputs "x", where x is an integer.
183+
void number(function<void(int)> printNumber) {
184+
while (index <= n) {
185+
std::lock_guard<std::mutex> lk(mtx);
186+
if (0 != index % 3 && 0 != index % 5 && index <= n) {
187+
printNumber(index);
188+
index++;
189+
}
190+
}
191+
}
192+
};
73193
```
74194
75195
### **...**

‎solution/1200-1299/1207.Unique Number of Occurrences/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ func uniqueOccurrences(arr []int) bool {
119119
}
120120
```
121121

122+
### **TypeScript**
123+
124+
```ts
125+
function uniqueOccurrences(arr: number[]): boolean {
126+
const cnt: Map<number, number> = new Map();
127+
for (const x of arr) {
128+
cnt.set(x, (cnt.get(x) || 0) + 1);
129+
}
130+
return cnt.size === new Set(cnt.values()).size;
131+
}
132+
```
133+
122134
### **...**
123135

124136
```

‎solution/1200-1299/1207.Unique Number of Occurrences/README_EN.md

+12
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,18 @@ func uniqueOccurrences(arr []int) bool {
104104
}
105105
```
106106

107+
### **TypeScript**
108+
109+
```ts
110+
function uniqueOccurrences(arr: number[]): boolean {
111+
const cnt: Map<number, number> = new Map();
112+
for (const x of arr) {
113+
cnt.set(x, (cnt.get(x) || 0) + 1);
114+
}
115+
return cnt.size === new Set(cnt.values()).size;
116+
}
117+
```
118+
107119
### **...**
108120

109121
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function uniqueOccurrences(arr: number[]): boolean {
2+
const cnt: Map<number, number> = new Map();
3+
for (const x of arr) {
4+
cnt.set(x, (cnt.get(x) || 0) + 1);
5+
}
6+
return cnt.size === new Set(cnt.values()).size;
7+
}

0 commit comments

Comments
 (0)