-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2649-NestedArrayGenerator.js
More file actions
99 lines (88 loc) · 2.83 KB
/
2649-NestedArrayGenerator.js
File metadata and controls
99 lines (88 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// 2649. Nested Array Generator
// Given a multi-dimensional array of integers,
// return a generator object which yields integers in the same order as inorder traversal.
// A multi-dimensional array is a recursive data structure that contains both integers and other multi-dimensional arrays.
// inorder traversal iterates over each array from left to right,
// yielding any integers it encounters or applying inorder traversal to any arrays it encounters.
// Example 1:
// Input: arr = [[[6]],[1,3],[]]
// Output: [6,1,3]
// Explanation:
// const generator = inorderTraversal(arr);
// generator.next().value; // 6
// generator.next().value; // 1
// generator.next().value; // 3
// generator.next().done; // true
// Example 2:
// Input: arr = []
// Output: []
// Explanation:
// There are no integers so the generator doesn't yield anything.
// Constraints:
// 0 <= arr.flat().length <= 10^5
// 0 <= arr.flat()[i] <= 10^5
// maxNestingDepth <= 10^5
/**
* @param {Array} arr
* @return {Generator}
*/
var inorderTraversal = function*(arr) {
// 递归把多维数据平铺成一维数组
const arrayFlatten = (array, ans = new Array()) => {
for(const item of array) {
if(Array.isArray(item)) {
arrayFlatten(item, ans);
} else {
ans.push(item);
}
}
return ans;
}
// 返回一个 Generator
for(const item of arrayFlatten(arr)) {
yield item;
}
};
// best solution
var inorderTraversal1 = function*(arr) {
const queue = []
for(let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
queue.unshift(...arr[i])
} else {
yield arr[i]
}
while(queue.length) {
const item = queue.shift()
if (Array.isArray(item)) {
queue.unshift(...item)
} else {
yield item
}
}
}
};
/**
* const gen = inorderTraversal([1, [2, 3]]);
* gen.next().value; // 1
* gen.next().value; // 2
* gen.next().value; // 3
*/
let gen = inorderTraversal([1, [2, 3]]);
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // 3
let generator = inorderTraversal([[[6]],[1,3],[]]);
console.log(generator.next().value); // 6
console.log(generator.next().value); // 1
console.log(generator.next().value); // 3
console.log(generator.next().done); // true
gen = inorderTraversal1([1, [2, 3]]);
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // 3
generator = inorderTraversal1([[[6]],[1,3],[]]);
console.log(generator.next().value); // 6
console.log(generator.next().value); // 1
console.log(generator.next().value); // 3
console.log(generator.next().done); // true