Skip to content

feat: add solutions to lc problems: No.1207,1214 #1579

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

Merged
merged 1 commit into from
Sep 6, 2023
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
6 changes: 3 additions & 3 deletions .github/workflows/compress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
id: calibre
uses: calibreapp/image-actions@main
with:
githubToken: ${{ secrets.ACTION_TOKEN }}
githubToken: ${{ secrets.GITHUB_TOKEN }}
# For non-Pull Requests, run in compressOnly mode and we'll PR after.
compressOnly: ${{ github.event_name != 'pull_request' }}
- name: Create Pull Request
Expand All @@ -44,7 +44,7 @@ jobs:
steps.calibre.outputs.markdown != ''
uses: peter-evans/create-pull-request@v4
with:
title: Auto Compress Images
title: 'chore: auto compress images'
branch-suffix: timestamp
commit-message: Compress Images
commit-message: 'chore: auto compress images'
body: ${{ steps.calibre.outputs.markdown }}
59 changes: 59 additions & 0 deletions solution/1100-1199/1195.Fizz Buzz Multithreaded/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,66 @@ class FizzBuzz {
<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class FizzBuzz {
private int n;

public FizzBuzz(int n) {
this.n = n;
}

private Semaphore fSema = new Semaphore(0);
private Semaphore bSema = new Semaphore(0);
private Semaphore fbSema = new Semaphore(0);
private Semaphore nSema = new Semaphore(1);

// printFizz.run() outputs "fizz".
public void fizz(Runnable printFizz) throws InterruptedException {
for (int i = 3; i <= n; i = i + 3) {
if (i % 5 != 0) {
fSema.acquire();
printFizz.run();
nSema.release();
}
}
}

// printBuzz.run() outputs "buzz".
public void buzz(Runnable printBuzz) throws InterruptedException {
for (int i = 5; i <= n; i = i + 5) {
if (i % 3 != 0) {
bSema.acquire();
printBuzz.run();
nSema.release();
}
}
}

// printFizzBuzz.run() outputs "fizzbuzz".
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
for (int i = 15; i <= n; i = i + 15) {
fbSema.acquire();
printFizzBuzz.run();
nSema.release();
}
}

// printNumber.accept(x) outputs "x", where x is an integer.
public void number(IntConsumer printNumber) throws InterruptedException {
for (int i = 1; i <= n; i++) {
nSema.acquire();
if (i % 3 == 0 && i % 5 == 0) {
fbSema.release();
} else if (i % 3 == 0) {
fSema.release();
} else if (i % 5 == 0) {
bSema.release();
} else {
printNumber.accept(i);
nSema.release();
}
}
}
}
```

### **C++**
Expand Down
120 changes: 120 additions & 0 deletions solution/1100-1199/1195.Fizz Buzz Multithreaded/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,127 @@
### **Java**

```java
class FizzBuzz {
private int n;

public FizzBuzz(int n) {
this.n = n;
}

private Semaphore fSema = new Semaphore(0);
private Semaphore bSema = new Semaphore(0);
private Semaphore fbSema = new Semaphore(0);
private Semaphore nSema = new Semaphore(1);

// printFizz.run() outputs "fizz".
public void fizz(Runnable printFizz) throws InterruptedException {
for (int i = 3; i <= n; i = i + 3) {
if (i % 5 != 0) {
fSema.acquire();
printFizz.run();
nSema.release();
}
}
}

// printBuzz.run() outputs "buzz".
public void buzz(Runnable printBuzz) throws InterruptedException {
for (int i = 5; i <= n; i = i + 5) {
if (i % 3 != 0) {
bSema.acquire();
printBuzz.run();
nSema.release();
}
}
}

// printFizzBuzz.run() outputs "fizzbuzz".
public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
for (int i = 15; i <= n; i = i + 15) {
fbSema.acquire();
printFizzBuzz.run();
nSema.release();
}
}

// printNumber.accept(x) outputs "x", where x is an integer.
public void number(IntConsumer printNumber) throws InterruptedException {
for (int i = 1; i <= n; i++) {
nSema.acquire();
if (i % 3 == 0 && i % 5 == 0) {
fbSema.release();
} else if (i % 3 == 0) {
fSema.release();
} else if (i % 5 == 0) {
bSema.release();
} else {
printNumber.accept(i);
nSema.release();
}
}
}
}
```

### **C++**

```cpp
class FizzBuzz {
private:
std::mutex mtx;
atomic<int> index;
int n;

public:
FizzBuzz(int n) {
this->n = n;
index = 1;
}

// printFizz() outputs "fizz".
void fizz(function<void()> printFizz) {
while (index <= n) {
std::lock_guard<std::mutex> lk(mtx);
if (0 == index % 3 && 0 != index % 5 && index <= n) {
printFizz();
index++;
}
}
}

// printBuzz() outputs "buzz".
void buzz(function<void()> printBuzz) {
while (index <= n) {
std::lock_guard<std::mutex> lk(mtx);
if (0 == index % 5 && 0 != index % 3 && index <= n) {
printBuzz();
index++;
}
}
}

// printFizzBuzz() outputs "fizzbuzz".
void fizzbuzz(function<void()> printFizzBuzz) {
while (index <= n) {
std::lock_guard<std::mutex> lk(mtx);
if (0 == index % 15 && index <= n) {
printFizzBuzz();
index++;
}
}
}

// printNumber(x) outputs "x", where x is an integer.
void number(function<void(int)> printNumber) {
while (index <= n) {
std::lock_guard<std::mutex> lk(mtx);
if (0 != index % 3 && 0 != index % 5 && index <= n) {
printNumber(index);
index++;
}
}
}
};
```

### **...**
Expand Down
12 changes: 12 additions & 0 deletions solution/1200-1299/1207.Unique Number of Occurrences/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ func uniqueOccurrences(arr []int) bool {
}
```

### **TypeScript**

```ts
function uniqueOccurrences(arr: number[]): boolean {
const cnt: Map<number, number> = new Map();
for (const x of arr) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
return cnt.size === new Set(cnt.values()).size;
}
```

### **...**

```
Expand Down
12 changes: 12 additions & 0 deletions solution/1200-1299/1207.Unique Number of Occurrences/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ func uniqueOccurrences(arr []int) bool {
}
```

### **TypeScript**

```ts
function uniqueOccurrences(arr: number[]): boolean {
const cnt: Map<number, number> = new Map();
for (const x of arr) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
return cnt.size === new Set(cnt.values()).size;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function uniqueOccurrences(arr: number[]): boolean {
const cnt: Map<number, number> = new Map();
for (const x of arr) {
cnt.set(x, (cnt.get(x) || 0) + 1);
}
return cnt.size === new Set(cnt.values()).size;
}
Loading