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. Python 安装cx_Oracle模块折腾笔记

    kali linux/ubuntu下安装: 不得不说安装这个模块很蛋疼,决定做个记录. sudo apt install build-essential unzip python-dev libaio ...

  2. tomcat同个端口配置多个项目后无后缀的页面跳转

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. nginx的高级配置和优化

    Nginx的高级配置(优化) 针对内核的配置优化 1)net.core.netdev_max_backlog 表示当网络接口接收数据包的速度大于内核处理这些包块的时候,允许发送到队列的数据包的最大数目 ...

  4. 【转】Linux逻辑卷管理

    一. 前言 LVM是逻辑卷管理(Logical Volume Manager)的简称,它是建立在物理存储设备之上的一个抽象层,允许你生成逻辑存储卷,与直接使用物理存储在管理上相比,提供了更好灵活性.L ...

  5. 【NOIP2016提高A组模拟9.9】闭门造车

    题目 自从htn体验了一把飙车的快感,他就下定决心要闭门造车!但是他两手空空怎么造得出车来呢?无奈的他只好来到了汽车零部件商店. 一走进商店,玲琅满目的各式零件看得htn眼花缭乱.但是他很快便反应过来 ...

  6. 算法——求n对()有多少种输出方式?

    letcode:22 Given n pairs of parentheses, write a function to generate all combinations of well-forme ...

  7. Python 函数Ⅲ

    默认参数 调用函数时,默认参数的值如果没有传入,则被认为是默认值.下例会打印默认的age,如果age没有被传入: 以上实例输出结果: 不定长参数 你可能需要一个函数能处理比当初声明时更多的参数.这些参 ...

  8. 12.24TG1

    1,线段树中把pushup写出去是因为 有点线段树维护的值比较多,写出去方便美观. 2,洛谷有的质量高有的质量不高,没办法.

  9. ansible-乱

    工作机制:ssh 无客户端 工作方式: 1,CMDB 2,公有云私有云API 3,使用ad-hoc 4,ansible-playbook ansible 执行命令,底层调用传输连接模块,将命令或文件传 ...

  10. C# 之 数组倒叙排列

    //倒叙排列 string temp=""; ; i < strlist.Length / ; i++) { temp = strlist[i]; strlist[i] = ...