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/originData/array-of-objects-to-matrix.json
2023-05-15 17:43:00 +08:00

63 lines
13 KiB
JSON
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.

{
"data": {
"question": {
"questionId": "2769",
"questionFrontendId": "2675",
"categoryTitle": "JavaScript",
"boundTopicId": 2267107,
"title": "Array of Objects to Matrix",
"titleSlug": "array-of-objects-to-matrix",
"content": "<p>Write a function that converts an array of objects&nbsp;<code>arr</code> into a matrix <code>m</code>.</p>\n\n<p><code>arr</code>&nbsp;is an array of objects or arrays. Each item in the array can be deeply nested with child arrays and child objects. It can also contain numbers, strings, booleans, and&nbsp;null values.</p>\n\n<p>The first row <code>m</code>&nbsp;should be the column names. If there is no nesting, the column names are the unique keys within the objects. If there is nesting, the column names&nbsp;are the respective paths in the object separated by <code>&quot;.&quot;</code>.</p>\n\n<p>Each of the remaining rows corresponds to an object in&nbsp;<code>arr</code>. Each value in the matrix corresponds to a value in an object. If a given object doesn&#39;t contain a value for a given column, the cell should contain an empty string&nbsp;<code>&quot;&quot;</code>.</p>\n\n<p>The colums in the matrix should be in <strong>lexographically ascending</strong> order.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr = [\n&nbsp; {&quot;b&quot;: 1, &quot;a&quot;: 2},\n&nbsp; {&quot;b&quot;: 3, &quot;a&quot;: 4}\n]\n<strong>Output:</strong> \n[\n&nbsp; [&quot;a&quot;, &quot;b&quot;],\n&nbsp; [2, 1],\n&nbsp; [4, 3]\n]\n\n<strong>Explanation:</strong>\nThere are two unique column names in the two objects: &quot;a&quot; and &quot;b&quot;.\n&quot;a&quot; corresponds with [2, 4].\n&quot;b&quot; coresponds with [1, 3].\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr = [\n&nbsp; {&quot;a&quot;: 1, &quot;b&quot;: 2},\n&nbsp; {&quot;c&quot;: 3, &quot;d&quot;: 4},\n&nbsp; {}\n]\n<strong>Output:</strong> \n[\n&nbsp; [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;],\n&nbsp; [1, 2, &quot;&quot;, &quot;&quot;],\n&nbsp; [&quot;&quot;, &quot;&quot;, 3, 4],\n&nbsp; [&quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;]\n]\n\n<strong>Explanation:</strong>\nThere are 4 unique column names: &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;.\nThe first object has values associated with &quot;a&quot; and &quot;b&quot;.\nThe second object has values associated with &quot;c&quot; and &quot;d&quot;.\nThe third object has no keys, so it is just a row of empty strings.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr = [\n&nbsp; {&quot;a&quot;: {&quot;b&quot;: 1, &quot;c&quot;: 2}},\n&nbsp; {&quot;a&quot;: {&quot;b&quot;: 3, &quot;d&quot;: 4}}\n]\n<strong>Output:</strong> \n[\n&nbsp; [&quot;a.b&quot;, &quot;a.c&quot;, &quot;a.d&quot;],\n&nbsp; [1, 2, &quot;&quot;],\n&nbsp; [3, &quot;&quot;, 4]\n]\n\n<strong>Explanation:</strong>\nIn this example, the objects are nested. The keys represent the full path to each value separated by periods.\nThere are three paths: &quot;a.b&quot;, &quot;a.c&quot;, &quot;a.d&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 4:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr = [\n&nbsp; [{&quot;a&quot;: null}],\n&nbsp; [{&quot;b&quot;: true}],\n&nbsp; [{&quot;c&quot;: &quot;x&quot;}]\n]\n<strong>Output:</strong> \n[\n&nbsp; [&quot;0.a&quot;, &quot;0.b&quot;, &quot;0.c&quot;],\n&nbsp; [null, &quot;&quot;, &quot;&quot;],\n&nbsp; [&quot;&quot;, true, &quot;&quot;],\n&nbsp; [&quot;&quot;, &quot;&quot;, &quot;x&quot;]\n]\n\n<strong>Explanation:</strong>\nArrays are also considered objects with their keys being their indices.\nEach array has one element so the keys are &quot;0.a&quot;, &quot;0.b&quot;, and &quot;0.c&quot;.\n</pre>\n\n<p><strong class=\"example\">Example 5:</strong></p>\n\n<pre>\n<strong>Input:</strong> \narr = [\n {},\n&nbsp; {},\n&nbsp; {},\n]\n<strong>Output:</strong> \n[\n&nbsp; [],\n&nbsp; [],\n&nbsp; [],\n&nbsp; []\n]\n\n<strong>Explanation:</strong>\nThere are no keys so every row is an empty array.</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= arr.length &lt;= 1000</code></li>\n\t<li><code>unique keys &lt;= 1000</code></li>\n</ul>\n",
"translatedTitle": "将对象数组转换为矩阵",
"translatedContent": "<p>编写一个函数,将对象数组&nbsp;<code>arr</code>&nbsp;转换为矩阵&nbsp;<code>m</code>&nbsp;。</p>\n\n<p><code>arr</code>&nbsp;是一个由对象组成的数组或一个数组。数组中的每个项都可以包含深层嵌套的子数组和子对象。它还可以包含数字、字符串、布尔值和空值。</p>\n\n<p>矩阵&nbsp;<code>m</code>&nbsp;的第一行应该是列名。如果没有嵌套,列名是对象中的唯一键。如果存在嵌套,列名是对象中相应路径,以点号&nbsp;<code>\".\"</code>&nbsp;分隔。</p>\n\n<p>剩余的每一行对应&nbsp;<code>arr</code>&nbsp;中的一个对象。矩阵中的每个值对应对象中的一个值。如果给定对象在给定列中没有值,则应该包含空字符串 <code>\"\"</code> 。</p>\n\n<p>矩阵中的列应按 <strong>字典升序</strong> 排列。</p>\n\n<p>&nbsp;</p>\n\n<p><strong class=\"example\">示例 1</strong></p>\n\n<pre>\n<b>输入:</b>\narr = [\n&nbsp; {\"b\": 1, \"a\": 2},\n&nbsp; {\"b\": 3, \"a\": 4}\n]\n<b>输出:</b>\n[\n&nbsp; [\"a\", \"b\"],\n&nbsp; [2, 1],\n&nbsp; [4, 3]\n]\n\n<strong>解释:</strong>\n两个对象中有两个唯一的列名\"a\"和\"b\"。 \n\"a\"对应[2, 4]。 \n\"b\"对应[1, 3]。\n</pre>\n\n<p><strong class=\"example\">示例 2</strong></p>\n\n<pre>\n<b>输入:</b>\narr = [\n&nbsp; {\"a\": 1, \"b\": 2},\n&nbsp; {\"c\": 3, \"d\": 4},\n&nbsp; {}\n]\n<b>输出:</b>\n[\n&nbsp; [\"a\", \"b\", \"c\", \"d\"],\n&nbsp; [1, 2, \"\", \"\"],\n&nbsp; [\"\", \"\", 3, 4],\n&nbsp; [\"\", \"\", \"\", \"\"]\n]\n\n<strong>解释:</strong>\n有四个唯一的列名\"a\"、\"b\"、\"c\"、\"d\"。 \n 第一个对象具有与\"a\"和\"b\"关联的值。 \n第二个对象具有与\"c\"和\"d\"关联的值。 \n第三个对象没有键因此只是一行空字符串。\n</pre>\n\n<p><strong class=\"example\">示例 3</strong></p>\n\n<pre>\n<b>输入:</b>\narr = [\n&nbsp; {\"a\": {\"b\": 1, \"c\": 2}},\n&nbsp; {\"a\": {\"b\": 3, \"d\": 4}}\n]\n<b>输出:</b>\n[\n&nbsp; [\"a.b\", \"a.c\", \"a.d\"],\n&nbsp; [1, 2, \"\"],\n&nbsp; [3, \"\", 4]\n]\n\n<b>解释:</b>\n在这个例子中对象是嵌套的。键表示每个值的完整路径路径之间用句点分隔。 \n有三个路径\"a.b\"、\"a.c\"、\"a.d\"。\n</pre>\n\n<p><strong class=\"example\">示例 4</strong></p>\n\n<pre>\n<b>输入:</b>\narr = [\n&nbsp; [{\"a\": null}],\n&nbsp; [{\"b\": true}],\n&nbsp; [{\"c\": \"x\"}]\n]\n<strong>输出:</strong> \n[\n&nbsp; [\"0.a\", \"0.b\", \"0.c\"],\n&nbsp; [null, \"\", \"\"],\n&nbsp; [\"\", true, \"\"],\n&nbsp; [\"\", \"\", \"x\"]\n]\n\n<strong>解释:</strong>\n数组也被视为具有索引为键的对象。 \n每个数组只有一个元素所以键是\"0.a\"、\"0.b\"和\"0.c\"。\n</pre>\n\n<p><strong class=\"example\">示例 5</strong></p>\n\n<pre>\n<b>输入:</b>\narr = [\n {},\n&nbsp; {},\n&nbsp; {},\n]\n<b>输出:</b>\n[\n&nbsp; [],\n&nbsp; [],\n&nbsp; [],\n&nbsp; []\n]\n\n<strong>解释:</strong>\n没有键所以每一行都是一个空数组。</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= arr.length &lt;= 1000</code></li>\n\t<li><code>unique keys &lt;= 1000</code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 0,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
"contributors": [],
"langToValidPlayground": "{\"cpp\": true, \"java\": true, \"python\": true, \"python3\": true, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"ruby\": false, \"bash\": false, \"swift\": false, \"golang\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"kotlin\": false, \"rust\": false, \"php\": false, \"typescript\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"dart\": false}",
"topicTags": [],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {Array} arr\n * @return {Matrix}\n */\nvar jsonToMatrix = function(arr) {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function jsonToMatrix(arr: any[]): (string | number | boolean | null)[] {\n\n};",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"54\", \"totalSubmission\": \"86\", \"totalAcceptedRaw\": 54, \"totalSubmissionRaw\": 86, \"acRate\": \"62.8%\"}",
"hints": [
"How could you split the problem up into sub-problems?",
"1.) Write a function that converts a single object into a dictionary that maps the path name to values. You can solve this recursively by keeping track of current path list.",
"2.) Write a function that converts a list of dictionaries into a matrix. Start by creating a list of all possible paths in any of the dictionaries. This will represent the columns."
],
"solution": null,
"status": null,
"sampleTestCase": "[{\"b\":1,\"a\":2},{\"b\":3,\"a\":4}]",
"metaData": "{\n \"name\": \"jsonToMatrix\",\n \"params\": [\n {\n \"name\": \"arr\",\n \"type\": \"string\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"languages\": [\n \"typescript\",\n \"javascript\"\n ],\n \"manual\": true\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"envInfo": "{\"javascript\":[\"JavaScript\",\"<p>\\u7248\\u672c\\uff1a<code>Node.js 16.13.2<\\/code><\\/p>\\r\\n\\r\\n<p>\\u60a8\\u7684\\u4ee3\\u7801\\u5728\\u6267\\u884c\\u65f6\\u5c06\\u5e26\\u4e0a <code>--harmony<\\/code> \\u6807\\u8bb0\\u6765\\u5f00\\u542f <a href=\\\"http:\\/\\/node.green\\/\\\" target=\\\"_blank\\\">\\u65b0\\u7248ES6\\u7279\\u6027<\\/a>\\u3002<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"],\"typescript\":[\"TypeScript\",\"<p>TypeScript 4.5.4<\\/p>\\r\\n\\r\\n<p>Compile Options: --alwaysStrict --strictBindCallApply --strictFunctionTypes --target ES2020<\\/p>\\r\\n\\r\\n<p><a href=\\\"https:\\/\\/lodash.com\\\" target=\\\"_blank\\\">lodash.js<\\/a> \\u5e93\\u5df2\\u7ecf\\u9ed8\\u8ba4\\u88ab\\u5305\\u542b\\u3002<\\/p>\\r\\n\\r\\n<p> \\u5982\\u9700\\u4f7f\\u7528\\u961f\\u5217\\/\\u4f18\\u5148\\u961f\\u5217\\uff0c\\u60a8\\u53ef\\u4f7f\\u7528 <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/priority-queue\\/tree\\/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\"> datastructures-js\\/priority-queue@5.3.0<\\/a> \\u548c <a href=\\\"https:\\/\\/github.com\\/datastructures-js\\/queue\\/tree\\/e63563025a5a805aa16928cb53bcd517bfea9230\\\" target=\\\"_blank\\\"> datastructures-js\\/queue@4.2.1<\\/a>\\u3002<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "[{\"b\":1,\"a\":2},{\"b\":3,\"a\":4}]\n[{\"a\":1,\"b\":2},{\"c\":3,\"d\":4},{}]\n[{\"a\":{\"b\":1,\"c\":2}},{\"a\":{\"b\":3,\"d\":4}}]\n[[{\"a\":null}],[{\"b\":true}],[{\"c\":\"x\"}]]\n[{},{},{}]",
"__typename": "QuestionNode"
}
}
}