{ "data": { "question": { "questionId": "1216", "questionFrontendId": "1116", "categoryTitle": "Concurrency", "boundTopicId": 13974, "title": "Print Zero Even Odd", "titleSlug": "print-zero-even-odd", "content": "

You have a function printNumber that can be called with an integer parameter and prints it to the console.

\n\n\n\n

You are given an instance of the class ZeroEvenOdd that has three functions: zero, even, and odd. The same instance of ZeroEvenOdd will be passed to three different threads:

\n\n\n\n

Modify the given class to output the series "010203040506..." where the length of the series must be 2n.

\n\n

Implement the ZeroEvenOdd class:

\n\n\n\n

 

\n

Example 1:

\n\n
\nInput: n = 2\nOutput: "0102"\nExplanation: There are three threads being fired asynchronously.\nOne of them calls zero(), the other calls even(), and the last one calls odd().\n"0102" is the correct output.\n
\n\n

Example 2:

\n\n
\nInput: n = 5\nOutput: "0102030405"\n
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": "打印零与奇偶数", "translatedContent": "

现有函数 printNumber 可以用一个整数参数调用,并输出该整数到控制台。

\n\n\n\n

给你类 ZeroEvenOdd 的一个实例,该类中有三个函数:zeroevenoddZeroEvenOdd 的相同实例将会传递给三个不同线程:

\n\n\n\n

修改给出的类,以输出序列 \"010203040506...\" ,其中序列的长度必须为 2n

\n\n

实现 ZeroEvenOdd 类:

\n\n\n\n

 

\n\n

示例 1:

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

示例 2:

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

 

\n\n

提示:

\n\n\n", "isPaidOnly": false, "difficulty": "Medium", "likes": 154, "dislikes": 0, "isLiked": null, "similarQuestions": "[{\"title\": \"Print FooBar Alternately\", \"titleSlug\": \"print-foobar-alternately\", \"difficulty\": \"Medium\", \"translatedTitle\": \"\\u4ea4\\u66ff\\u6253\\u5370 FooBar\", \"isPaidOnly\": false}]", "contributors": [], "langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}", "topicTags": [ { "name": "Concurrency", "slug": "concurrency", "translatedName": "多线程", "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "C++", "langSlug": "cpp", "code": "class ZeroEvenOdd {\nprivate:\n int n;\n\npublic:\n ZeroEvenOdd(int n) {\n this->n = n;\n }\n\n // printNumber(x) outputs \"x\", where x is an integer.\n void zero(function printNumber) {\n \n }\n\n void even(function printNumber) {\n \n }\n\n void odd(function printNumber) {\n \n }\n};", "__typename": "CodeSnippetNode" }, { "lang": "Java", "langSlug": "java", "code": "class ZeroEvenOdd {\n private int n;\n \n public ZeroEvenOdd(int n) {\n this.n = n;\n }\n\n // printNumber.accept(x) outputs \"x\", where x is an integer.\n public void zero(IntConsumer printNumber) throws InterruptedException {\n \n }\n\n public void even(IntConsumer printNumber) throws InterruptedException {\n \n }\n\n public void odd(IntConsumer printNumber) throws InterruptedException {\n \n }\n}", "__typename": "CodeSnippetNode" }, { "lang": "Python", "langSlug": "python", "code": "class ZeroEvenOdd(object):\n def __init__(self, n):\n self.n = n\n \n \n\t# printNumber(x) outputs \"x\", where x is an integer.\n def zero(self, printNumber):\n \"\"\"\n :type printNumber: method\n :rtype: void\n \"\"\"\n \n \n \n def even(self, printNumber):\n \"\"\"\n :type printNumber: method\n :rtype: void\n \"\"\"\n \n \n \n def odd(self, printNumber):\n \"\"\"\n :type printNumber: method\n :rtype: void\n \"\"\"\n \n ", "__typename": "CodeSnippetNode" }, { "lang": "Python3", "langSlug": "python3", "code": "class ZeroEvenOdd:\n def __init__(self, n):\n self.n = n\n \n \n\t# printNumber(x) outputs \"x\", where x is an integer.\n def zero(self, printNumber: 'Callable[[int], None]') -> None:\n \n \n \n def even(self, printNumber: 'Callable[[int], None]') -> None:\n \n \n \n def odd(self, printNumber: 'Callable[[int], None]') -> None:\n \n ", "__typename": "CodeSnippetNode" }, { "lang": "C", "langSlug": "c", "code": "typedef struct {\n int n;\n} ZeroEvenOdd;\n\nZeroEvenOdd* zeroEvenOddCreate(int n) {\n ZeroEvenOdd* obj = (ZeroEvenOdd*) malloc(sizeof(ZeroEvenOdd));\n obj->n = n;\n return obj;\n}\n\n// You may call global function `void printNumber(int x)`\n// to output \"x\", where x is an integer.\n\nvoid zero(ZeroEvenOdd* obj) {\n \n}\n\nvoid even(ZeroEvenOdd* obj) {\n \n}\n\nvoid odd(ZeroEvenOdd* obj) {\n \n}\n\nvoid zeroEvenOddFree(ZeroEvenOdd* obj) {\n \n}", "__typename": "CodeSnippetNode" }, { "lang": "C#", "langSlug": "csharp", "code": "public class ZeroEvenOdd {\n private int n;\n \n public ZeroEvenOdd(int n) {\n this.n = n;\n }\n\n // printNumber(x) outputs \"x\", where x is an integer.\n public void Zero(Action printNumber) {\n \n }\n\n public void Even(Action printNumber) {\n \n }\n\n public void Odd(Action printNumber) {\n \n }\n}", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"36.5K\", \"totalSubmission\": \"66.4K\", \"totalAcceptedRaw\": 36485, \"totalSubmissionRaw\": 66426, \"acRate\": \"54.9%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "2", "metaData": "{\n \"name\": \"printZeroEvenOdd\",\n \"params\": [\n {\n \"name\": \"n\",\n \"type\": \"integer\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n },\n \"languages\": [\n \"java\",\n \"python\",\n \"python3\",\n \"cpp\",\n \"c\",\n \"csharp\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "envInfo": "{\"cpp\":[\"C++\",\"

\\u7248\\u672c\\uff1aclang 11<\\/code> \\u91c7\\u7528\\u6700\\u65b0C++ 20\\u6807\\u51c6\\u3002<\\/p>\\r\\n\\r\\n

\\u7f16\\u8bd1\\u65f6\\uff0c\\u5c06\\u4f1a\\u91c7\\u7528-O2<\\/code>\\u7ea7\\u4f18\\u5316\\u3002AddressSanitizer<\\/a> \\u4e5f\\u88ab\\u5f00\\u542f\\u6765\\u68c0\\u6d4bout-of-bounds<\\/code>\\u548cuse-after-free<\\/code>\\u9519\\u8bef\\u3002<\\/p>\\r\\n\\r\\n

\\u4e3a\\u4e86\\u4f7f\\u7528\\u65b9\\u4fbf\\uff0c\\u5927\\u90e8\\u5206\\u6807\\u51c6\\u5e93\\u7684\\u5934\\u6587\\u4ef6\\u5df2\\u7ecf\\u88ab\\u81ea\\u52a8\\u5bfc\\u5165\\u3002<\\/p>\"],\"java\":[\"Java\",\"

\\u7248\\u672c\\uff1aOpenJDK 17<\\/code>\\u3002\\u53ef\\u4ee5\\u4f7f\\u7528Java 8\\u7684\\u7279\\u6027\\u4f8b\\u5982\\uff0clambda expressions \\u548c stream API\\u3002<\\/p>\\r\\n\\r\\n

\\u4e3a\\u4e86\\u65b9\\u4fbf\\u8d77\\u89c1\\uff0c\\u5927\\u90e8\\u5206\\u6807\\u51c6\\u5e93\\u7684\\u5934\\u6587\\u4ef6\\u5df2\\u88ab\\u5bfc\\u5165\\u3002<\\/p>\\r\\n\\r\\n

\\u5305\\u542b Pair \\u7c7b: https:\\/\\/docs.oracle.com\\/javase\\/8\\/javafx\\/api\\/javafx\\/util\\/Pair.html <\\/p>\"],\"python\":[\"Python\",\"

\\u7248\\u672c\\uff1a Python 2.7.12<\\/code><\\/p>\\r\\n\\r\\n

\\u4e3a\\u4e86\\u65b9\\u4fbf\\u8d77\\u89c1\\uff0c\\u5927\\u90e8\\u5206\\u5e38\\u7528\\u5e93\\u5df2\\u7ecf\\u88ab\\u81ea\\u52a8 \\u5bfc\\u5165\\uff0c\\u5982\\uff1aarray<\\/a>, bisect<\\/a>, collections<\\/a>\\u3002\\u5982\\u679c\\u60a8\\u9700\\u8981\\u4f7f\\u7528\\u5176\\u4ed6\\u5e93\\u51fd\\u6570\\uff0c\\u8bf7\\u81ea\\u884c\\u5bfc\\u5165\\u3002<\\/p>\\r\\n\\r\\n

\\u6ce8\\u610f Python 2.7 \\u5c06\\u57282020\\u5e74\\u540e\\u4e0d\\u518d\\u7ef4\\u62a4<\\/a>\\u3002 \\u5982\\u60f3\\u4f7f\\u7528\\u6700\\u65b0\\u7248\\u7684Python\\uff0c\\u8bf7\\u9009\\u62e9Python 3\\u3002<\\/p>\"],\"c\":[\"C\",\"

\\u7248\\u672c\\uff1aGCC 8.2<\\/code>\\uff0c\\u91c7\\u7528GNU11\\u6807\\u51c6\\u3002<\\/p>\\r\\n\\r\\n

\\u7f16\\u8bd1\\u65f6\\uff0c\\u5c06\\u4f1a\\u91c7\\u7528-O1<\\/code>\\u7ea7\\u4f18\\u5316\\u3002 AddressSanitizer<\\/a>\\u4e5f\\u88ab\\u5f00\\u542f\\u6765\\u68c0\\u6d4bout-of-bounds<\\/code>\\u548cuse-after-free<\\/code>\\u9519\\u8bef\\u3002<\\/p>\\r\\n\\r\\n

\\u4e3a\\u4e86\\u4f7f\\u7528\\u65b9\\u4fbf\\uff0c\\u5927\\u90e8\\u5206\\u6807\\u51c6\\u5e93\\u7684\\u5934\\u6587\\u4ef6\\u5df2\\u7ecf\\u88ab\\u81ea\\u52a8\\u5bfc\\u5165\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u60f3\\u4f7f\\u7528\\u54c8\\u5e0c\\u8868\\u8fd0\\u7b97, \\u60a8\\u53ef\\u4ee5\\u4f7f\\u7528 uthash<\\/a>\\u3002 \\\"uthash.h\\\"\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5bfc\\u5165\\u3002\\u8bf7\\u770b\\u5982\\u4e0b\\u793a\\u4f8b:<\\/p>\\r\\n\\r\\n

1. \\u5f80\\u54c8\\u5e0c\\u8868\\u4e2d\\u6dfb\\u52a0\\u4e00\\u4e2a\\u5bf9\\u8c61\\uff1a<\\/b>\\r\\n

\\r\\nstruct hash_entry {\\r\\n    int id;            \\/* we'll use this field as the key *\\/\\r\\n    char name[10];\\r\\n    UT_hash_handle hh; \\/* makes this structure hashable *\\/\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n    HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n<\\/pre>\\r\\n<\\/p>\\r\\n\\r\\n

2. \\u5728\\u54c8\\u5e0c\\u8868\\u4e2d\\u67e5\\u627e\\u4e00\\u4e2a\\u5bf9\\u8c61\\uff1a<\\/b>\\r\\n

\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n    struct hash_entry *s;\\r\\n    HASH_FIND_INT(users, &user_id, s);\\r\\n    return s;\\r\\n}\\r\\n<\\/pre>\\r\\n<\\/p>\\r\\n\\r\\n

3. \\u4ece\\u54c8\\u5e0c\\u8868\\u4e2d\\u5220\\u9664\\u4e00\\u4e2a\\u5bf9\\u8c61\\uff1a<\\/b>\\r\\n

\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n    HASH_DEL(users, user);  \\r\\n}\\r\\n<\\/pre>\\r\\n<\\/p>\"],\"csharp\":[\"C#\",\"

C# 10<\\/a> \\u8fd0\\u884c\\u5728 .NET 6 \\u4e0a<\\/p>\"],\"python3\":[\"Python3\",\"

\\u7248\\u672c\\uff1aPython 3.10<\\/code><\\/p>\\r\\n\\r\\n

\\u4e3a\\u4e86\\u65b9\\u4fbf\\u8d77\\u89c1\\uff0c\\u5927\\u90e8\\u5206\\u5e38\\u7528\\u5e93\\u5df2\\u7ecf\\u88ab\\u81ea\\u52a8 \\u5bfc\\u5165\\uff0c\\u5982array<\\/a>, bisect<\\/a>, collections<\\/a>\\u3002 \\u5982\\u679c\\u60a8\\u9700\\u8981\\u4f7f\\u7528\\u5176\\u4ed6\\u5e93\\u51fd\\u6570\\uff0c\\u8bf7\\u81ea\\u884c\\u5bfc\\u5165\\u3002<\\/p>\\r\\n\\r\\n

\\u5982\\u9700\\u4f7f\\u7528 Map\\/TreeMap \\u6570\\u636e\\u7ed3\\u6784\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 sortedcontainers<\\/a> \\u5e93\\u3002<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "exampleTestcases": "2\n5", "__typename": "QuestionNode" } } }