1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/交替打印 FooBar [print-foobar-alternately].html
2022-03-29 12:43:11 +08:00

53 lines
1.2 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个类:</p>
<pre>
class FooBar {
public void foo() {
&nbsp; &nbsp; for (int i = 0; i &lt; n; i++) {
&nbsp; &nbsp; &nbsp; print("foo");
&nbsp; }
}
public void bar() {
&nbsp; &nbsp; for (int i = 0; i &lt; n; i++) {
&nbsp; &nbsp; &nbsp; print("bar");
&nbsp; &nbsp; }
}
}
</pre>
<p>两个不同的线程将会共用一个 <code>FooBar</code>&nbsp;实例:</p>
<ul>
<li>线程 A 将会调用&nbsp;<code>foo()</code>&nbsp;方法,而</li>
<li>线程 B 将会调用&nbsp;<code>bar()</code>&nbsp;方法</li>
</ul>
<p>请设计修改程序,以确保 <code>"foobar"</code> 被输出 <code>n</code> 次。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>n = 1
<strong>输出:</strong>"foobar"
<strong>解释:</strong>这里有两个线程被异步启动。其中一个调用 foo() 方法, 另一个调用 bar() 方法,"foobar" 将被输出一次。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>n = 2
<strong>输出:</strong>"foobarfoobar"
<strong>解释:</strong>"foobar" 将被输出两次。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 1000</code></li>
</ul>