LeetCode_1116

假设有这么一个类:

class ZeroEvenOdd {
public ZeroEvenOdd(int n) { ... } // 构造函数
public void zero(printNumber) { ... } // 仅打印出 0
public void even(printNumber) { ... } // 仅打印出 偶数
public void odd(printNumber) { ... } // 仅打印出 奇数
}
相同的一个 ZeroEvenOdd 类实例将会传递给三个不同的线程:

线程 A 将调用 zero(),它只输出 0 。
线程 B 将调用 even(),它只输出偶数。
线程 C 将调用 odd(),它只输出奇数。
每个线程都有一个 printNumber 方法来输出一个整数。
请修改给出的代码以输出整数序列 010203040506... ,其中序列的长度必须为 2n。

示例 1:

输入:n = 2
输出:"0102"
说明:三条线程异步执行,其中一个调用 zero(),另一个线程调用 even(),
最后一个线程调用 odd()。正确的输出为 "0102"。

示例 2:

输入:n = 5
输出:"0102030405"

示例代码:

class ZeroEvenOdd {
private int n; public ZeroEvenOdd(int n) {
this.n = n;
} // printNumber.accept(x) outputs "x", where x is an integer.
public void zero(IntConsumer printNumber) throws InterruptedException { } public void even(IntConsumer printNumber) throws InterruptedException { } public void odd(IntConsumer printNumber) throws InterruptedException { }
}

方法一:使用信号量题解

  • 测试用例:14个
  • 执行用时:10ms
  • 内存消耗:36.7MB
import java.util.concurrent.Semaphore;

class ZeroEvenOdd {
private int n;
private Semaphore s1, s2, s3; public ZeroEvenOdd(int n) {
this.n = n;
s1 = new Semaphore(1);
s2 = new Semaphore(0);
s3 = new Semaphore(0);
} // printNumber.accept(x) outputs "x", where x is an integer.
public void zero(IntConsumer printNumber) throws InterruptedException {
for (int i = 1; i <= n; i++) {
s1.acquire();
printNumber.accept(0);
if ((i & 1) == 0) {
s2.release();
} else {
s3.release();
}
}
} public void even(IntConsumer printNumber) throws InterruptedException {
for (int i = 2; i <= n; i+=2) {
s2.acquire();
printNumber.accept(i);
s1.release();
}
} public void odd(IntConsumer printNumber) throws InterruptedException {
for (int i = 1; i <= n; i+=2) {
s3.acquire();
printNumber.accept(i);
s1.release();
}
}
}

LeetCode_1116.打印零与奇偶数(多线程)的更多相关文章

  1. LeetCode:打印零与奇偶数【1116】

    LeetCode:打印零与奇偶数[1116] 题目描述 假设有这么一个类: class ZeroEvenOdd { public ZeroEvenOdd(int n) { ... } // 构造函数 ...

  2. C#判断奇偶数的函數

    // 现代流行的"程序员" public static bool IsOdd(int n) { while (true) { switch (n) { : return true; ...

  3. js判断奇偶数实现隐藏显示功能 与css立体按钮

      hello!   好久不见了 ,今天也没准备什么技术,知识想和大家就见个面,一个js判断奇数偶数来实现css样式 ,感觉最大的用途就是页面的导航.就这么一个小小的技术. 劳动快乐   当!当!当! ...

  4. (转)CSS3:nth-child()伪类选择器,奇偶数行自定义样式first-child

    原文地址 Table表格奇偶数行定义样式: CSS3的强大,让人惊叹,人们在惊喜之余,又不得不为其艰难的道路感到可惜:好的标准只有得到行业浏览器的良好支持才算得上“标准”.CSS3标 准已提出数年,但 ...

  5. 奇偶数判断2(if else+switch语句)

    public class 奇偶数判断2 { public static void main(String [] agrs){ float s = 17f; //定义浮点型数据s float h = s ...

  6. 奇偶数判断1(if,else if语句)

    public class 奇偶数判断 { public static void main(String [] args){ float s = 9f; //取单浮点型变量s,可为任意值 float h ...

  7. CSS3:nth-child()伪类选择器,Table表格奇偶数行定义样式

    转自爱设计 原文链接http://www.dangshopex.com/jishufenxiang/WEBkaifajishu/8653.html CSS3的强大,让人惊叹,人们在惊喜之余,又不得不为 ...

  8. 利用奇偶数来获取websocket推送时间间隔(或者比较前一个数和下一个数的变化)

    利用奇偶数来获取websocket推送时间间隔(或者比较前一个数和下一个数的变化) 在vue中的 data () {     return { countTime: 0,         newDat ...

  9. 【九度OJ】题目1169:比较奇偶数个数 解题报告

    [九度OJ]题目1169:比较奇偶数个数 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1169 题目描述: 第一行输入一个数,为n, ...

随机推荐

  1. Solution for automatic update of Chinese word segmentation full-text index in NEO4J

    Solution for automatic update of Chinese word segmentation full-text index in NEO4J 1. Sample data 2 ...

  2. ArcGIS 在VS2010中 ESRI.ArcGIS.SOESupport.dll 无法正常加载的处理

    转自  http://blog.csdn.net/tnt123688/article/details/23186973 问题描述: 打开ArcGIS的SOE模板后,提示  错误 命名空间“ESRI.A ...

  3. vue点击出现蒙版

      需求: 1.点击一个事件时弹出一个蒙版: 2.蒙版上有取消,删除事件:(点击取消时候蒙版消失,点击删除时,删除蒙版并消失): 3.点击空白地方,蒙版也消失:   <template> ...

  4. AQtime使用

    今天刚到网上下了AQtime.因为有个通信及数据存储的程序出现的内存泄漏.在用户的环境里出现了两次,在测试的环境里一次也没有出现,开发人员猜测是一部分代码引起的.说要代码的覆盖测试.看看测试环境里有那 ...

  5. python cv2展示网络图片、图片编解码、及与base64转换

    从网络读取图像数据并展示 需要使用cv2.imdecode()函数,从指定的内存缓存中读取数据,并把数据转换(解码)成图像格式:主要用于从网络传输数据中恢复出图像. # -*- coding: utf ...

  6. SourceTree报错

    1.SourceTree  拉取报错 Your local changes to the following files would be overwritten by merge:XXX 原因: 本 ...

  7. 为Xcode配置Git和Github

    Xcode.Git和Github是三个伟大的编程工具.本文记录一下如何在Xcode中使用Git作为源代码控制工具,以及如何将本地的Git仓库和远程Github上的仓库集成起来. 1. 如何为新建的Xc ...

  8. kali下纯文本与窗口环境切换

    切到纯文本环境,想返回 试了半天ctrl+alt+f7不行,  最后我想试试ctrl+alt+f8竟然成了: 而且那是之前以root账户登录图像界面时切换回去是f8,普通用户是f9 ,为何如此,我还不 ...

  9. java 统计字符串中连续重复的字符,并得出新字符串

    题目: 比如输入为aaabbc,输出a3b2c1 完整解答: public class Other { static String func(String str) { StringBuffer re ...

  10. [project X] tiny210(s5pv210)上电启动流程(BL0-BL2)(转)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/ooonebook/article/det ...