File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -529,6 +529,7 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
529
529
| [ 2636. Promise Pool] [ lc2636 ] | 🟠 Medium | [ ![ js] ( res/js.png )] [ lc2636js ] |
530
530
| [ 2637. Promise Time Limit] [ lc2637 ] | 🟢 Easy | [ ![ js] ( res/js.png )] [ lc2637js ] |
531
531
| [ 2648. Generate Fibonacci Sequence] [ lc2648 ] | 🟢 Easy | [ ![ js] ( res/js.png )] [ lc2648js ] |
532
+ | [ 2649. Nested Array Generator] [ lc2649 ] | 🟠 Medium | [ ![ js] ( res/js.png )] [ lc2649js ] |
532
533
| [ 2665. Counter II] [ lc2665 ] | 🟢 Easy | [ ![ js] ( res/js.png )] [ lc2665js ] |
533
534
| [ 2666. Allow One Function Call] [ lc2666 ] | 🟢 Easy | [ ![ js] ( res/js.png )] [ lc2666js ] |
534
535
| [ 2667. Create Hello World Function] [ lc2667 ] | 🟢 Easy | [ ![ js] ( res/js.png )] [ lc2667js ] |
@@ -1686,6 +1687,8 @@ Solutions to LeetCode problems. The first column links to the problem in LeetCod
1686
1687
[ lc2637js ] : leetcode/promise-time-limit.js
1687
1688
[ lc2648 ] : https://leetcode.com/problems/generate-fibonacci-sequence/
1688
1689
[ lc2648js ] : leetcode/generate-fibonacci-sequence.js
1690
+ [ lc2649 ] : https://leetcode.com/problems/nested-array-generator/
1691
+ [ lc2649js ] : leetcode/nested-array-generator.js
1689
1692
[ lc2665 ] : https://leetcode.com/problems/counter-ii/
1690
1693
[ lc2665js ] : leetcode/counter-ii.js
1691
1694
[ lc2666 ] : https://leetcode.com/problems/allow-one-function-call/
Original file line number Diff line number Diff line change
1
+ // 2649. Nested Array Generator
2
+ // 🟠 Medium
3
+ //
4
+ // https://leetcode.com/problems/nested-array-generator/
5
+ //
6
+ // Tags: Javascript
7
+
8
+ // A generator that returns values of a nested array as if produced by an
9
+ // inorder traversal.
10
+ //
11
+ // Time complexity: O(n) - Each call takes O(1), n calls will take O(n)
12
+ // Space complexity: O(n) - The call stack could grow to size n.
13
+ //
14
+ // Runtime 184 ms Beats 73.7%
15
+ // Memory 74 MB Beats 50.62%
16
+ /**
17
+ * @param {Array } arr
18
+ * @return {Generator }
19
+ */
20
+ var inorderTraversal = function * ( arr ) {
21
+ if ( ! Array . isArray ( arr ) ) {
22
+ yield arr ;
23
+ return ;
24
+ }
25
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
26
+ yield * inorderTraversal ( arr [ i ] ) ;
27
+ }
28
+ } ;
29
+
30
+ /**
31
+ * const gen = inorderTraversal([1, [2, 3]]);
32
+ * gen.next().value; // 1
33
+ * gen.next().value; // 2
34
+ * gen.next().value; // 3
35
+ */
You can’t perform that action at this time.
0 commit comments