首页 > 其他分享 >LeetCode 1115. Print FooBar Alternately

LeetCode 1115. Print FooBar Alternately

时间:2022-10-14 12:33:39浏览次数:39  
标签:bar int FooBar 1115 Print run foo public

原题链接在这里:https://leetcode.com/problems/print-foobar-alternately/

题目:

Suppose you are given the following code:

class FooBar {
  public void foo() {
    for (int i = 0; i < n; i++) {
      print("foo");
    }
  }

  public void bar() {
    for (int i = 0; i < n; i++) {
      print("bar");
    }
  }
}

The same instance of FooBar will be passed to two different threads:

  • thread A will call foo(), while
  • thread B will call bar().

Modify the given program to output "foobar" n times.

Example 1:

Input: n = 1
Output: "foobar"
Explanation: There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar().
"foobar" is being output 1 time.

Example 2:

Input: n = 2
Output: "foobarfoobar"
Explanation: "foobar" is being output 2 times. 

Constraints:

  • 1 <= n <= 1000

题解:

We could use Semaphore. One Semaphore for foo has count 1, then first acquire() don't need to wait. 

The other Semaphore for bar has count 0, then first acquire() needs to wait.

Time Complexity: O(1).

Space: O(1).

AC Java:

 1 class FooBar {
 2     private int n;
 3     private Semaphore s0;
 4     private Semaphore s1;
 5     
 6     public FooBar(int n) {
 7         this.n = n;
 8         this.s0 = new Semaphore(0);
 9         this.s1 = new Semaphore(1);
10     }
11 
12     public void foo(Runnable printFoo) throws InterruptedException {
13         
14         for (int i = 0; i < n; i++) {
15             s1.acquire();
16             
17             // printFoo.run() outputs "foo". Do not change or remove this line.
18             printFoo.run();
19             s0.release();
20         }
21     }
22 
23     public void bar(Runnable printBar) throws InterruptedException {
24         
25         for (int i = 0; i < n; i++) {
26             s0.acquire();
27             
28             // printBar.run() outputs "bar". Do not change or remove this line.
29             printBar.run();
30             s1.release();
31         }
32     }
33 }

Could also use synchronized.

Time Complexity: O(1).

Space: O(1).

AC Java:

 1 class FooBar {
 2     private int n;
 3     private boolean fooRunning;
 4     
 5     public FooBar(int n) {
 6         this.n = n;
 7         this.fooRunning = false;
 8     }
 9 
10     public void foo(Runnable printFoo) throws InterruptedException {
11         
12         for (int i = 0; i < n; i++) {
13             synchronized(this){
14                 while(fooRunning){
15                     this.wait();
16                 }
17                 
18                 // printFoo.run() outputs "foo". Do not change or remove this line.
19                 printFoo.run();
20                 fooRunning = true;
21                 this.notifyAll();
22             }
23             
24         }
25     }
26 
27     public void bar(Runnable printBar) throws InterruptedException {
28         
29         for (int i = 0; i < n; i++) {
30             synchronized(this){
31                 while(!fooRunning){
32                     this.wait();
33                 }
34                 
35                 // printBar.run() outputs "bar". Do not change or remove this line.
36                 printBar.run();
37                 fooRunning = false;
38                 this.notifyAll();
39             }
40         }
41     }
42 }

 

标签:bar,int,FooBar,1115,Print,run,foo,public
From: https://www.cnblogs.com/Dylan-Java-NYC/p/16791241.html

相关文章

  • LeetCode 1116. Print Zero Even Odd
    原题链接在这里:https://leetcode.com/problems/print-zero-even-odd/题目:Youhaveafunction printNumber thatcanbecalledwithanintegerparameterandprints......
  • printf函数
    printf是指格式化输出函数,主要功能是向标准输出设备按规定格式输出信息。printf是C语言标准库函数,定义于头文件<stdio.h>。printf函数的一般调用格式为:printf("<格式......
  • sprintboot常用工具汇总
    如何获取springboot中的application.properties文件中配置的信息MySQL8连接JDBCspringboot的配置文件......
  • shell 知识点补充(3)-修改语系/特殊字符/ printf/sed 工具/awk 工具/diff/cmp
    1、修改语系的方法为:[root@testroot]#LANG=en             (根据情况指定为其它语法,如:C)[root@testroot]#exportLANGlinuxvi删除指定所有字符按一下esc......
  • IDEA jsp 写Java脚本的时候不能使用out.print()问题
    IDEAjsp写Java脚本的时候不能使用out.print()问题参考:ideajsp无法使用out.print方法_NoBug的博客-CSDN博客_ideajspout问题:  解决:File->ProjectStructure......
  • python将print的数据输出到txt文件中
    前言:在写一些小的测试脚本时,需要查看一些日志,我们不会去搭建一个logger工具;而是选择直接输出到txt文件中,测试完后,也方便查看结果。在需要输出打印前面,打开txt文件,以追加的......
  • Using a Robot to Print the Lexicographically Smallest String
    UsingaRobottoPrinttheLexicographicallySmallestStringYouaregivenastring s andarobotthatcurrentlyholdsanemptystring t .Applyoneofthe......
  • LeetCode 2434. Using a Robot to Print the Lexicographically Smallest String
    原题链接在这里:https://leetcode.com/problems/using-a-robot-to-print-the-lexicographically-smallest-string/题目:Youaregivenastring s andarobotthatcurr......
  • 聊聊foobar是什么?
    聊聊foobar是什么?大一时看很多老外的文档,总是看到一个叫foobar的词,当时倒不是纠结这个词到底是什么意思,因为看到这个词使用的场景大多是代码示例段、示例变量名等一些无意......
  • 详解 printf() 函数
    声明(叠甲):鄙人水平有限,本文章仅供参考。1.引子#include<stdio.h>intmain(){printf("helloworld\n");return0;}上面这一段代码大家应该都十分的......