mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			128 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			128 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>编写一个函数,将对象数组 <code>arr</code> 转换为矩阵 <code>m</code> 。</p>
 | 
						||
 | 
						||
<p><code>arr</code> 是一个由对象组成的数组或一个数组。数组中的每个项都可以包含深层嵌套的子数组和子对象。它还可以包含数字、字符串、布尔值和空值。</p>
 | 
						||
 | 
						||
<p>矩阵 <code>m</code> 的第一行应该是列名。如果没有嵌套,列名是对象中的唯一键。如果存在嵌套,列名是对象中相应路径,以点号 <code>"."</code> 分隔。</p>
 | 
						||
 | 
						||
<p>剩余的每一行对应 <code>arr</code> 中的一个对象。矩阵中的每个值对应对象中的一个值。如果给定对象在给定列中没有值,则应该包含空字符串 <code>""</code> 。</p>
 | 
						||
 | 
						||
<p>矩阵中的列应按 <strong>字典升序</strong> 排列。</p>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong class="example">示例 1:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<b>输入:</b>
 | 
						||
arr = [
 | 
						||
  {"b": 1, "a": 2},
 | 
						||
  {"b": 3, "a": 4}
 | 
						||
]
 | 
						||
<b>输出:</b>
 | 
						||
[
 | 
						||
  ["a", "b"],
 | 
						||
  [2, 1],
 | 
						||
  [4, 3]
 | 
						||
]
 | 
						||
 | 
						||
<strong>解释:</strong>
 | 
						||
两个对象中有两个唯一的列名:"a"和"b"。 
 | 
						||
"a"对应[2, 4]。 
 | 
						||
"b"对应[1, 3]。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong class="example">示例 2:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<b>输入:</b>
 | 
						||
arr = [
 | 
						||
  {"a": 1, "b": 2},
 | 
						||
  {"c": 3, "d": 4},
 | 
						||
  {}
 | 
						||
]
 | 
						||
<b>输出:</b>
 | 
						||
[
 | 
						||
  ["a", "b", "c", "d"],
 | 
						||
  [1, 2, "", ""],
 | 
						||
  ["", "", 3, 4],
 | 
						||
  ["", "", "", ""]
 | 
						||
]
 | 
						||
 | 
						||
<strong>解释:</strong>
 | 
						||
有四个唯一的列名:"a"、"b"、"c"、"d"。 
 | 
						||
 第一个对象具有与"a"和"b"关联的值。 
 | 
						||
第二个对象具有与"c"和"d"关联的值。 
 | 
						||
第三个对象没有键,因此只是一行空字符串。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong class="example">示例 3:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<b>输入:</b>
 | 
						||
arr = [
 | 
						||
  {"a": {"b": 1, "c": 2}},
 | 
						||
  {"a": {"b": 3, "d": 4}}
 | 
						||
]
 | 
						||
<b>输出:</b>
 | 
						||
[
 | 
						||
  ["a.b", "a.c", "a.d"],
 | 
						||
  [1, 2, ""],
 | 
						||
  [3, "", 4]
 | 
						||
]
 | 
						||
 | 
						||
<b>解释:</b>
 | 
						||
在这个例子中,对象是嵌套的。键表示每个值的完整路径,路径之间用句点分隔。 
 | 
						||
有三个路径:"a.b"、"a.c"、"a.d"。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong class="example">示例 4:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<b>输入:</b>
 | 
						||
arr = [
 | 
						||
  [{"a": null}],
 | 
						||
  [{"b": true}],
 | 
						||
  [{"c": "x"}]
 | 
						||
]
 | 
						||
<strong>输出:</strong> 
 | 
						||
[
 | 
						||
  ["0.a", "0.b", "0.c"],
 | 
						||
  [null, "", ""],
 | 
						||
  ["", true, ""],
 | 
						||
  ["", "", "x"]
 | 
						||
]
 | 
						||
 | 
						||
<strong>解释:</strong>
 | 
						||
数组也被视为具有索引为键的对象。 
 | 
						||
每个数组只有一个元素,所以键是"0.a"、"0.b"和"0.c"。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong class="example">示例 5:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<b>输入:</b>
 | 
						||
arr = [
 | 
						||
  {},
 | 
						||
  {},
 | 
						||
  {},
 | 
						||
]
 | 
						||
<b>输出:</b>
 | 
						||
[
 | 
						||
  [],
 | 
						||
  [],
 | 
						||
  [],
 | 
						||
  []
 | 
						||
]
 | 
						||
 | 
						||
<strong>解释:</strong>
 | 
						||
没有键,所以每一行都是一个空数组。</pre>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>1 <= arr.length <= 1000</code></li>
 | 
						||
	<li><code>unique keys <= 1000</code></li>
 | 
						||
</ul>
 |