Эх сурвалжийг харах

#feat:leetcode p1195题题解

yangyi 2 долоо хоног өмнө
parent
commit
279f1e99cb

+ 71 - 0
src/leetcode/p1195/FizzBuzz.java

@@ -0,0 +1,71 @@
+package leetcode.p1195;
+
+import java.util.concurrent.locks.ReentrantLock;
+import java.util.function.IntConsumer;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: FizzBuzz
+ * @Author: 杨逸
+ * @Data:2026/3/28 10:54
+ * @Description: https://leetcode.cn/problems/fizz-buzz-multithreaded/description/
+ * 1195. 交替打印字符串
+ */
+public class FizzBuzz {
+    private final int n;
+    private volatile int val;
+    private final ReentrantLock lock;
+    public FizzBuzz(int n) {
+        this.n = n;
+        val =1;
+        lock = new ReentrantLock();
+    }
+
+    // printFizz.run() outputs "fizz".
+    public void fizz(Runnable printFizz) throws InterruptedException {
+        while (val <= n){
+            lock.lock();
+            if(val % 3 == 0 && val % 5 != 0 && val <= n){
+                printFizz.run();
+                val++;
+            }
+            lock.unlock();
+        }
+    }
+
+    // printBuzz.run() outputs "buzz".
+    public void buzz(Runnable printBuzz) throws InterruptedException {
+        while (val <= n){
+            lock.lock();
+            if(val % 5 == 0 && val % 3 != 0 && val <= n){
+                printBuzz.run();
+                val++;
+            }
+            lock.unlock();
+        }
+    }
+
+    // printFizzBuzz.run() outputs "fizzbuzz".
+    public void fizzbuzz(Runnable printFizzBuzz) throws InterruptedException {
+        while (val <= n){
+            lock.lock();
+            if(val % 5 == 0 && val % 3 == 0 && val <= n){
+                printFizzBuzz.run();
+                val++;
+            }
+            lock.unlock();
+        }
+    }
+
+    // printNumber.accept(x) outputs "x", where x is an integer.
+    public void number(IntConsumer printNumber) throws InterruptedException {
+        while (val <= n){
+            lock.lock();
+            if(val % 5 != 0 && val % 3 != 0 && val <= n){
+                printNumber.accept(val);
+                val++;
+            }
+            lock.unlock();
+        }
+    }
+}

+ 66 - 0
src/leetcode/p1195/FizzBuzzTest.java

@@ -0,0 +1,66 @@
+package leetcode.p1195;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: FizzBuzzTest
+ * @Author: 杨逸
+ * @Data:2026/3/28 11:08
+ * @Description:
+ */
+@RunWith(Parameterized.class)
+public class FizzBuzzTest {
+    private final int n;
+
+    public FizzBuzzTest(int n) {
+        this.n = n;
+    }
+    @Parameterized.Parameters
+    public static Collection<Object[]> data(){
+        return Arrays.asList(new Object[][]{
+                {15},
+                {20},
+                {1000},
+                {10000},
+        });
+    }
+
+    @Test(timeout = 1000L)
+    public void test() {
+        FizzBuzz fizzBuzz = new FizzBuzz(n);
+        new Thread(()->{
+            try {
+                fizzBuzz.fizz(() -> System.out.println("fizz"));
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }).start();
+        new Thread(()->{
+            try {
+                fizzBuzz.buzz(() -> System.out.println("buzz"));
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }).start();
+        new Thread(()->{
+            try {
+                fizzBuzz.fizzbuzz(() -> System.out.println("fizzbuzz"));
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }).start();
+        new Thread(()->{
+            try {
+                fizzBuzz.number(System.out::println);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }).start();
+    }
+}