|
1 |
| -# [2675. Array of Objects to Matrix](https://leetcode.cn/problems/array-of-objects-to-matrix) |
| 1 | +# [2675. 将对象数组转换为矩阵](https://leetcode.cn/problems/array-of-objects-to-matrix) |
2 | 2 |
|
3 | 3 | [English Version](/solution/2600-2699/2675.Array%20of%20Objects%20to%20Matrix/README_EN.md)
|
4 | 4 |
|
5 | 5 | ## 题目描述
|
6 | 6 |
|
7 | 7 | <!-- 这里写题目描述 -->
|
8 | 8 |
|
9 |
| -<p>Write a function that converts an array of objects <code>arr</code> into a matrix <code>m</code>.</p> |
| 9 | +<p>编写一个函数,将对象数组 <code>arr</code> 转换为矩阵 <code>m</code> 。</p> |
10 | 10 |
|
11 |
| -<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> |
| 11 | +<p><code>arr</code> 是一个由对象组成的数组或一个数组。数组中的每个项都可以包含深层嵌套的子数组和子对象。它还可以包含数字、字符串、布尔值和空值。</p> |
12 | 12 |
|
13 |
| -<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> |
| 13 | +<p>矩阵 <code>m</code> 的第一行应该是列名。如果没有嵌套,列名是对象中的唯一键。如果存在嵌套,列名是对象中相应路径,以点号 <code>"."</code> 分隔。</p> |
14 | 14 |
|
15 |
| -<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> |
| 15 | +<p>剩余的每一行对应 <code>arr</code> 中的一个对象。矩阵中的每个值对应对象中的一个值。如果给定对象在给定列中没有值,则应该包含空字符串 <code>""</code> 。</p> |
16 | 16 |
|
17 |
| -<p>The colums in the matrix should be in <strong>lexographically ascending</strong> order.</p> |
| 17 | +<p>矩阵中的列应按 <strong>字典升序</strong> 排列。</p> |
18 | 18 |
|
19 | 19 | <p> </p>
|
20 |
| -<p><strong class="example">Example 1:</strong></p> |
| 20 | + |
| 21 | +<p><strong class="example">示例 1:</strong></p> |
21 | 22 |
|
22 | 23 | <pre>
|
23 |
| -<strong>Input:</strong> |
| 24 | +<b>输入:</b> |
24 | 25 | arr = [
|
25 |
| - {"b": 1, "a": 2}, |
26 |
| - {"b": 3, "a": 4} |
| 26 | + {"b": 1, "a": 2}, |
| 27 | + {"b": 3, "a": 4} |
27 | 28 | ]
|
28 |
| -<strong>Output:</strong> |
| 29 | +<b>输出:</b> |
29 | 30 | [
|
30 |
| - ["a", "b"], |
| 31 | + ["a", "b"], |
31 | 32 | [2, 1],
|
32 | 33 | [4, 3]
|
33 | 34 | ]
|
34 | 35 |
|
35 |
| -<strong>Explanation:</strong> |
36 |
| -There are two unique column names in the two objects: "a" and "b". |
37 |
| -"a" corresponds with [2, 4]. |
38 |
| -"b" coresponds with [1, 3]. |
| 36 | +<strong>解释:</strong> |
| 37 | +两个对象中有两个唯一的列名:"a"和"b"。 |
| 38 | +"a"对应[2, 4]。 |
| 39 | +"b"对应[1, 3]。 |
39 | 40 | </pre>
|
40 | 41 |
|
41 |
| -<p><strong class="example">Example 2:</strong></p> |
| 42 | +<p><strong class="example">示例 2:</strong></p> |
42 | 43 |
|
43 | 44 | <pre>
|
44 |
| -<strong>Input:</strong> |
| 45 | +<b>输入:</b> |
45 | 46 | arr = [
|
46 |
| - {"a": 1, "b": 2}, |
47 |
| - {"c": 3, "d": 4}, |
| 47 | + {"a": 1, "b": 2}, |
| 48 | + {"c": 3, "d": 4}, |
48 | 49 | {}
|
49 | 50 | ]
|
50 |
| -<strong>Output:</strong> |
| 51 | +<b>输出:</b> |
51 | 52 | [
|
52 |
| - ["a", "b", "c", "d"], |
53 |
| - [1, 2, "", ""], |
54 |
| - ["", "", 3, 4], |
55 |
| - ["", "", "", ""] |
| 53 | + ["a", "b", "c", "d"], |
| 54 | + [1, 2, "", ""], |
| 55 | + ["", "", 3, 4], |
| 56 | + ["", "", "", ""] |
56 | 57 | ]
|
57 | 58 |
|
58 |
| -<strong>Explanation:</strong> |
59 |
| -There are 4 unique column names: "a", "b", "c", "d". |
60 |
| -The first object has values associated with "a" and "b". |
61 |
| -The second object has values associated with "c" and "d". |
62 |
| -The third object has no keys, so it is just a row of empty strings. |
| 59 | +<strong>解释:</strong> |
| 60 | +有四个唯一的列名:"a"、"b"、"c"、"d"。 |
| 61 | + 第一个对象具有与"a"和"b"关联的值。 |
| 62 | +第二个对象具有与"c"和"d"关联的值。 |
| 63 | +第三个对象没有键,因此只是一行空字符串。 |
63 | 64 | </pre>
|
64 | 65 |
|
65 |
| -<p><strong class="example">Example 3:</strong></p> |
| 66 | +<p><strong class="example">示例 3:</strong></p> |
66 | 67 |
|
67 | 68 | <pre>
|
68 |
| -<strong>Input:</strong> |
| 69 | +<b>输入:</b> |
69 | 70 | arr = [
|
70 |
| - {"a": {"b": 1, "c": 2}}, |
71 |
| - {"a": {"b": 3, "d": 4}} |
| 71 | + {"a": {"b": 1, "c": 2}}, |
| 72 | + {"a": {"b": 3, "d": 4}} |
72 | 73 | ]
|
73 |
| -<strong>Output:</strong> |
| 74 | +<b>输出:</b> |
74 | 75 | [
|
75 |
| - ["a.b", "a.c", "a.d"], |
76 |
| - [1, 2, ""], |
77 |
| - [3, "", 4] |
| 76 | + ["a.b", "a.c", "a.d"], |
| 77 | + [1, 2, ""], |
| 78 | + [3, "", 4] |
78 | 79 | ]
|
79 | 80 |
|
80 |
| -<strong>Explanation:</strong> |
81 |
| -In this example, the objects are nested. The keys represent the full path to each value separated by periods. |
82 |
| -There are three paths: "a.b", "a.c", "a.d". |
| 81 | +<b>解释:</b> |
| 82 | +在这个例子中,对象是嵌套的。键表示每个值的完整路径,路径之间用句点分隔。 |
| 83 | +有三个路径:"a.b"、"a.c"、"a.d"。 |
83 | 84 | </pre>
|
84 | 85 |
|
85 |
| -<p><strong class="example">Example 4:</strong></p> |
| 86 | +<p><strong class="example">示例 4:</strong></p> |
86 | 87 |
|
87 | 88 | <pre>
|
88 |
| -<strong>Input:</strong> |
| 89 | +<b>输入:</b> |
89 | 90 | arr = [
|
90 |
| - [{"a": null}], |
91 |
| - [{"b": true}], |
92 |
| - [{"c": "x"}] |
| 91 | + [{"a": null}], |
| 92 | + [{"b": true}], |
| 93 | + [{"c": "x"}] |
93 | 94 | ]
|
94 |
| -<strong>Output:</strong> |
| 95 | +<strong>输出:</strong> |
95 | 96 | [
|
96 |
| - ["0.a", "0.b", "0.c"], |
97 |
| - [null, "", ""], |
98 |
| - ["", true, ""], |
99 |
| - ["", "", "x"] |
| 97 | + ["0.a", "0.b", "0.c"], |
| 98 | + [null, "", ""], |
| 99 | + ["", true, ""], |
| 100 | + ["", "", "x"] |
100 | 101 | ]
|
101 | 102 |
|
102 |
| -<strong>Explanation:</strong> |
103 |
| -Arrays are also considered objects with their keys being their indices. |
104 |
| -Each array has one element so the keys are "0.a", "0.b", and "0.c". |
| 103 | +<strong>解释:</strong> |
| 104 | +数组也被视为具有索引为键的对象。 |
| 105 | +每个数组只有一个元素,所以键是"0.a"、"0.b"和"0.c"。 |
105 | 106 | </pre>
|
106 | 107 |
|
107 |
| -<p><strong class="example">Example 5:</strong></p> |
| 108 | +<p><strong class="example">示例 5:</strong></p> |
108 | 109 |
|
109 | 110 | <pre>
|
110 |
| -<strong>Input:</strong> |
| 111 | +<b>输入:</b> |
111 | 112 | arr = [
|
112 | 113 | {},
|
113 | 114 | {},
|
114 | 115 | {},
|
115 | 116 | ]
|
116 |
| -<strong>Output:</strong> |
| 117 | +<b>输出:</b> |
117 | 118 | [
|
118 | 119 | [],
|
119 | 120 | [],
|
120 | 121 | [],
|
121 | 122 | []
|
122 | 123 | ]
|
123 | 124 |
|
124 |
| -<strong>Explanation:</strong> |
125 |
| -There are no keys so every row is an empty array.</pre> |
| 125 | +<strong>解释:</strong> |
| 126 | +没有键,所以每一行都是一个空数组。</pre> |
126 | 127 |
|
127 | 128 | <p> </p>
|
128 |
| -<p><strong>Constraints:</strong></p> |
| 129 | + |
| 130 | +<p><strong>提示:</strong></p> |
129 | 131 |
|
130 | 132 | <ul>
|
131 | 133 | <li><code>1 <= arr.length <= 1000</code></li>
|
|
0 commit comments