mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
126 lines
4.0 KiB
HTML
126 lines
4.0 KiB
HTML
|
<p>Write a function that converts an array of objects <code>arr</code> into a matrix <code>m</code>.</p>
|
||
|
|
||
|
<p><code>arr</code> 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 null values.</p>
|
||
|
|
||
|
<p>The first row <code>m</code> 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 are the respective paths in the object separated by <code>"."</code>.</p>
|
||
|
|
||
|
<p>Each of the remaining rows corresponds to an object in <code>arr</code>. Each value in the matrix corresponds to a value in an object. If a given object doesn't contain a value for a given column, the cell should contain an empty string <code>""</code>.</p>
|
||
|
|
||
|
<p>The colums in the matrix should be in <strong>lexographically ascending</strong> order.</p>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong class="example">Example 1:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong>
|
||
|
arr = [
|
||
|
{"b": 1, "a": 2},
|
||
|
{"b": 3, "a": 4}
|
||
|
]
|
||
|
<strong>Output:</strong>
|
||
|
[
|
||
|
["a", "b"],
|
||
|
[2, 1],
|
||
|
[4, 3]
|
||
|
]
|
||
|
|
||
|
<strong>Explanation:</strong>
|
||
|
There are two unique column names in the two objects: "a" and "b".
|
||
|
"a" corresponds with [2, 4].
|
||
|
"b" coresponds with [1, 3].
|
||
|
</pre>
|
||
|
|
||
|
<p><strong class="example">Example 2:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong>
|
||
|
arr = [
|
||
|
{"a": 1, "b": 2},
|
||
|
{"c": 3, "d": 4},
|
||
|
{}
|
||
|
]
|
||
|
<strong>Output:</strong>
|
||
|
[
|
||
|
["a", "b", "c", "d"],
|
||
|
[1, 2, "", ""],
|
||
|
["", "", 3, 4],
|
||
|
["", "", "", ""]
|
||
|
]
|
||
|
|
||
|
<strong>Explanation:</strong>
|
||
|
There are 4 unique column names: "a", "b", "c", "d".
|
||
|
The first object has values associated with "a" and "b".
|
||
|
The second object has values associated with "c" and "d".
|
||
|
The third object has no keys, so it is just a row of empty strings.
|
||
|
</pre>
|
||
|
|
||
|
<p><strong class="example">Example 3:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong>
|
||
|
arr = [
|
||
|
{"a": {"b": 1, "c": 2}},
|
||
|
{"a": {"b": 3, "d": 4}}
|
||
|
]
|
||
|
<strong>Output:</strong>
|
||
|
[
|
||
|
["a.b", "a.c", "a.d"],
|
||
|
[1, 2, ""],
|
||
|
[3, "", 4]
|
||
|
]
|
||
|
|
||
|
<strong>Explanation:</strong>
|
||
|
In this example, the objects are nested. The keys represent the full path to each value separated by periods.
|
||
|
There are three paths: "a.b", "a.c", "a.d".
|
||
|
</pre>
|
||
|
|
||
|
<p><strong class="example">Example 4:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong>
|
||
|
arr = [
|
||
|
[{"a": null}],
|
||
|
[{"b": true}],
|
||
|
[{"c": "x"}]
|
||
|
]
|
||
|
<strong>Output:</strong>
|
||
|
[
|
||
|
["0.a", "0.b", "0.c"],
|
||
|
[null, "", ""],
|
||
|
["", true, ""],
|
||
|
["", "", "x"]
|
||
|
]
|
||
|
|
||
|
<strong>Explanation:</strong>
|
||
|
Arrays are also considered objects with their keys being their indices.
|
||
|
Each array has one element so the keys are "0.a", "0.b", and "0.c".
|
||
|
</pre>
|
||
|
|
||
|
<p><strong class="example">Example 5:</strong></p>
|
||
|
|
||
|
<pre>
|
||
|
<strong>Input:</strong>
|
||
|
arr = [
|
||
|
{},
|
||
|
{},
|
||
|
{},
|
||
|
]
|
||
|
<strong>Output:</strong>
|
||
|
[
|
||
|
[],
|
||
|
[],
|
||
|
[],
|
||
|
[]
|
||
|
]
|
||
|
|
||
|
<strong>Explanation:</strong>
|
||
|
There are no keys so every row is an empty array.</pre>
|
||
|
|
||
|
<p> </p>
|
||
|
<p><strong>Constraints:</strong></p>
|
||
|
|
||
|
<ul>
|
||
|
<li><code>1 <= arr.length <= 1000</code></li>
|
||
|
<li><code>unique keys <= 1000</code></li>
|
||
|
</ul>
|