Skip to content

Commit aa8ed86

Browse files
authored
refactor: update solution to lc problem: No.2625 (doocs#2848)
1 parent 41ebef4 commit aa8ed86

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

solution/2600-2699/2625.Flatten Deeply Nested Array/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ n = 2
8383

8484
我们可以使用递归的方法,将多维数组扁平化。
8585

86-
在函数中,我们首先判断 $n$ 是否小于等于 $0$,如果是,直接返回原数组。否则,我们遍历数组的每个元素 $x$,如果 $x$ 是数组,我们递归调用函数,将 $x$ 作为参数,$n - 1$ 作为深度,将返回值添加到结果数组中;否则,将 $x$ 添加到结果数组中。最后返回结果数组。
86+
在函数中,我们首先判断 $n$ 是否小于等于 $0$,如果是,直接返回原数组。否则,我们遍历数组的每个元素 $x$,如果 $x$ 是数组,我们递归调用函数,参数为 $(x, n - 1)$,将返回值添加到结果数组中;否则,将 $x$ 添加到结果数组中。最后返回结果数组。
8787

8888
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的元素个数。
8989

@@ -95,12 +95,12 @@ n = 2
9595
type MultiDimensionalArray = (number | MultiDimensionalArray)[];
9696

9797
var flat = function (arr: MultiDimensionalArray, n: number): MultiDimensionalArray {
98-
if (n <= 0) {
98+
if (!n) {
9999
return arr;
100100
}
101101
const ans: MultiDimensionalArray = [];
102102
for (const x of arr) {
103-
if (Array.isArray(x)) {
103+
if (Array.isArray(x) && n) {
104104
ans.push(...flat(x, n - 1));
105105
} else {
106106
ans.push(x);

solution/2600-2699/2625.Flatten Deeply Nested Array/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ The maximum depth of any subarray is 1. Thus, all of them are flattened.</pre>
8686
type MultiDimensionalArray = (number | MultiDimensionalArray)[];
8787

8888
var flat = function (arr: MultiDimensionalArray, n: number): MultiDimensionalArray {
89-
if (n <= 0) {
89+
if (!n) {
9090
return arr;
9191
}
9292
const ans: MultiDimensionalArray = [];
9393
for (const x of arr) {
94-
if (Array.isArray(x)) {
94+
if (Array.isArray(x) && n) {
9595
ans.push(...flat(x, n - 1));
9696
} else {
9797
ans.push(x);

solution/2600-2699/2625.Flatten Deeply Nested Array/Solution.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
type MultiDimensionalArray = (number | MultiDimensionalArray)[];
22

33
var flat = function (arr: MultiDimensionalArray, n: number): MultiDimensionalArray {
4-
if (n <= 0) {
4+
if (!n) {
55
return arr;
66
}
77
const ans: MultiDimensionalArray = [];
88
for (const x of arr) {
9-
if (Array.isArray(x)) {
9+
if (Array.isArray(x) && n) {
1010
ans.push(...flat(x, n - 1));
1111
} else {
1212
ans.push(x);

0 commit comments

Comments
 (0)