Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update solution to lc problem: No.2625 #2848

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions solution/2600-2699/2625.Flatten Deeply Nested Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ n = 2

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

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

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

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

var flat = function (arr: MultiDimensionalArray, n: number): MultiDimensionalArray {
if (n <= 0) {
if (!n) {
return arr;
}
const ans: MultiDimensionalArray = [];
for (const x of arr) {
if (Array.isArray(x)) {
if (Array.isArray(x) && n) {
ans.push(...flat(x, n - 1));
} else {
ans.push(x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ The maximum depth of any subarray is 1. Thus, all of them are flattened.</pre>
type MultiDimensionalArray = (number | MultiDimensionalArray)[];

var flat = function (arr: MultiDimensionalArray, n: number): MultiDimensionalArray {
if (n <= 0) {
if (!n) {
return arr;
}
const ans: MultiDimensionalArray = [];
for (const x of arr) {
if (Array.isArray(x)) {
if (Array.isArray(x) && n) {
ans.push(...flat(x, n - 1));
} else {
ans.push(x);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
type MultiDimensionalArray = (number | MultiDimensionalArray)[];

var flat = function (arr: MultiDimensionalArray, n: number): MultiDimensionalArray {
if (n <= 0) {
if (!n) {
return arr;
}
const ans: MultiDimensionalArray = [];
for (const x of arr) {
if (Array.isArray(x)) {
if (Array.isArray(x) && n) {
ans.push(...flat(x, n - 1));
} else {
ans.push(x);
Expand Down
Loading