Skip to content

Commit f87d4b0

Browse files
authored
feat: add solutions to lc problems: No.2803,2804 (doocs#1385)
* No.2803.Factorial Generator * No.2804.Array Prototype ForEach
1 parent 5d2d454 commit f87d4b0

File tree

12 files changed

+571
-0
lines changed

12 files changed

+571
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# [2803. Factorial Generator](https://leetcode.cn/problems/factorial-generator)
2+
3+
[English Version](/solution/2800-2899/2803.Factorial%20Generator/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Write a generator function that takes an integer <code>n</code> as an argument and returns a generator object which yields the <strong>factorial sequence</strong>.</p>
10+
11+
<p>The&nbsp;<strong>factorial sequence</strong>&nbsp;is defined by the relation <code>n!&nbsp;= n *&nbsp;<span style="font-size: 13px;">(</span>n-1)&nbsp;* (n-2)&nbsp;*&nbsp;...&nbsp;* 2 * 1​​​.</code></p>
12+
13+
<p>The factorial of 0 is defined as 1.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> n = 5
20+
<strong>Output:</strong> [1,2,6,24,120]
21+
<strong>Explanation:</strong>
22+
const gen = factorial(5)
23+
gen.next().value // 1
24+
gen.next().value // 2
25+
gen.next().value // 6
26+
gen.next().value // 24
27+
gen.next().value // 120
28+
</pre>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> n = 2
34+
<strong>Output:</strong> [1,2]
35+
<strong>Explanation:</strong>
36+
const gen = factorial(2)
37+
gen.next().value // 1
38+
gen.next().value // 2
39+
</pre>
40+
41+
<p><strong class="example">Example 3:</strong></p>
42+
43+
<pre>
44+
<strong>Input:</strong> n = 0
45+
<strong>Output:</strong> [1]
46+
<strong>Explanation:</strong>
47+
const gen = factorial(0)
48+
gen.next().value // 1
49+
</pre>
50+
51+
<p>&nbsp;</p>
52+
<p><strong>Constraints:</strong></p>
53+
54+
<ul>
55+
<li><code>0 &lt;= n &lt;= 18</code></li>
56+
</ul>
57+
58+
## 解法
59+
60+
<!-- 这里可写通用的实现逻辑 -->
61+
62+
<!-- tabs:start -->
63+
64+
### **TypeScript**
65+
66+
<!-- 这里可写当前语言的特殊实现逻辑 -->
67+
68+
```ts
69+
function* factorial(n: number): Generator<number> {
70+
if (n === 0) {
71+
yield 1;
72+
}
73+
let ans = 1;
74+
for (let i = 1; i <= n; ++i) {
75+
ans *= i;
76+
yield ans;
77+
}
78+
}
79+
80+
/**
81+
* const gen = factorial(2);
82+
* gen.next().value; // 1
83+
* gen.next().value; // 2
84+
*/
85+
```
86+
87+
<!-- tabs:end -->
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# [2803. Factorial Generator](https://leetcode.com/problems/factorial-generator)
2+
3+
[中文文档](/solution/2800-2899/2803.Factorial%20Generator/README.md)
4+
5+
## Description
6+
7+
<p>Write a generator function that takes an integer <code>n</code> as an argument and returns a generator object which yields the <strong>factorial sequence</strong>.</p>
8+
9+
<p>The&nbsp;<strong>factorial sequence</strong>&nbsp;is defined by the relation <code>n!&nbsp;= n *&nbsp;<span style="font-size: 13px;">(</span>n-1)&nbsp;* (n-2)&nbsp;*&nbsp;...&nbsp;* 2 * 1​​​.</code></p>
10+
11+
<p>The factorial of 0 is defined as 1.</p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> n = 5
18+
<strong>Output:</strong> [1,2,6,24,120]
19+
<strong>Explanation:</strong>
20+
const gen = factorial(5)
21+
gen.next().value // 1
22+
gen.next().value // 2
23+
gen.next().value // 6
24+
gen.next().value // 24
25+
gen.next().value // 120
26+
</pre>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> n = 2
32+
<strong>Output:</strong> [1,2]
33+
<strong>Explanation:</strong>
34+
const gen = factorial(2)
35+
gen.next().value // 1
36+
gen.next().value // 2
37+
</pre>
38+
39+
<p><strong class="example">Example 3:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong> n = 0
43+
<strong>Output:</strong> [1]
44+
<strong>Explanation:</strong>
45+
const gen = factorial(0)
46+
gen.next().value // 1
47+
</pre>
48+
49+
<p>&nbsp;</p>
50+
<p><strong>Constraints:</strong></p>
51+
52+
<ul>
53+
<li><code>0 &lt;= n &lt;= 18</code></li>
54+
</ul>
55+
56+
## Solutions
57+
58+
<!-- tabs:start -->
59+
60+
### **TypeScript**
61+
62+
```ts
63+
function* factorial(n: number): Generator<number> {
64+
if (n === 0) {
65+
yield 1;
66+
}
67+
let ans = 1;
68+
for (let i = 1; i <= n; ++i) {
69+
ans *= i;
70+
yield ans;
71+
}
72+
}
73+
74+
/**
75+
* const gen = factorial(2);
76+
* gen.next().value; // 1
77+
* gen.next().value; // 2
78+
*/
79+
```
80+
81+
<!-- tabs:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function* factorial(n: number): Generator<number> {
2+
if (n === 0) {
3+
yield 1;
4+
}
5+
let ans = 1;
6+
for (let i = 1; i <= n; ++i) {
7+
ans *= i;
8+
yield ans;
9+
}
10+
}
11+
12+
/**
13+
* const gen = factorial(2);
14+
* gen.next().value; // 1
15+
* gen.next().value; // 2
16+
*/
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# [2804. Array Prototype ForEach](https://leetcode.cn/problems/array-prototype-foreach)
2+
3+
[English Version](/solution/2800-2899/2804.Array%20Prototype%20ForEach/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Write your version of method&nbsp;<code>forEach</code>&nbsp;that enhances all arrays such that you can call the&nbsp;<code>array.forEach(callback, context)</code>&nbsp;method on any array and it will execute <code>callback</code> on each element of the array.&nbsp;Method&nbsp;<code>forEach</code> should not return anything.</p>
10+
11+
<p><code>callback</code> accepts the following arguments:</p>
12+
13+
<ul>
14+
<li><code>value</code> -&nbsp;represents the current element being processed in the array. It is the value of the element in the current iteration.</li>
15+
<li><code>index</code> -&nbsp;represents the index of the current element being processed in the array.</li>
16+
<li><code>array</code> -&nbsp;represents the array itself, allowing access to the entire array within the callback function.</li>
17+
</ul>
18+
19+
<p>The <code>context</code> is the object that should be passed as the function context parameter to the <code>callback</code> function, ensuring that the <code>this</code>&nbsp;keyword within the <code>callback</code> function refers to this <code>context</code> object.</p>
20+
21+
<p>Try to implement it without using the built-in array methods.</p>
22+
23+
<p>&nbsp;</p>
24+
<p><strong class="example">Example 1:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong>
28+
arr = [1,2,3],
29+
callback = (val, i, arr) =&gt; arr[i] = val * 2,
30+
context = {&quot;context&quot;:true}
31+
<strong>Output:</strong> [2,4,6]
32+
<strong>Explanation:</strong>
33+
arr.forEach(callback, context)&nbsp;
34+
console.log(arr) // [2,4,6]
35+
36+
The callback is executed on each element of the array.
37+
</pre>
38+
39+
<p><strong class="example">Example 2:</strong></p>
40+
41+
<pre>
42+
<strong>Input:</strong>
43+
arr = [true, true, false, false],
44+
callback = (val, i, arr) =&gt; arr[i] = this,
45+
context = {&quot;context&quot;: false}
46+
<strong>Output:</strong> [{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false}]
47+
<strong>Explanation:</strong>
48+
arr.forEach(callback, context)&nbsp;
49+
console.log(arr) // [{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false}]
50+
51+
The callback is executed on each element of the array with the right context.
52+
</pre>
53+
54+
<p><strong class="example">Example 3:</strong></p>
55+
56+
<pre>
57+
<strong>Input:</strong>
58+
arr = [true, true, false, false],
59+
callback = (val, i, arr) =&gt; arr[i] = !val,
60+
context = {&quot;context&quot;: 5}
61+
<strong>Output:</strong> [false,false,true,true]
62+
</pre>
63+
64+
<p>&nbsp;</p>
65+
<p><strong>Constraints:</strong></p>
66+
67+
<ul>
68+
<li><code>arr</code> is a valid JSON array</li>
69+
<li><code>context</code> is a valid JSON object</li>
70+
<li><code>fn</code> is a function</li>
71+
<li><code>0 &lt;= arr.length &lt;= 10<sup>5</sup></code></li>
72+
</ul>
73+
74+
## 解法
75+
76+
<!-- 这里可写通用的实现逻辑 -->
77+
78+
<!-- tabs:start -->
79+
80+
### **TypeScript**
81+
82+
<!-- 这里可写当前语言的特殊实现逻辑 -->
83+
84+
```ts
85+
Array.prototype.forEach = function (callback: Function, context: any): void {
86+
for (let i = 0; i < this.length; ++i) {
87+
callback.call(context, this[i], i, this);
88+
}
89+
};
90+
91+
/**
92+
* const arr = [1,2,3];
93+
* const callback = (val, i, arr) => arr[i] = val * 2;
94+
* const context = {"context":true};
95+
*
96+
* arr.forEach(callback, context)
97+
*
98+
* console.log(arr) // [2,4,6]
99+
*/
100+
```
101+
102+
<!-- tabs:end -->
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# [2804. Array Prototype ForEach](https://leetcode.com/problems/array-prototype-foreach)
2+
3+
[中文文档](/solution/2800-2899/2804.Array%20Prototype%20ForEach/README.md)
4+
5+
## Description
6+
7+
<p>Write your version of method&nbsp;<code>forEach</code>&nbsp;that enhances all arrays such that you can call the&nbsp;<code>array.forEach(callback, context)</code>&nbsp;method on any array and it will execute <code>callback</code> on each element of the array.&nbsp;Method&nbsp;<code>forEach</code> should not return anything.</p>
8+
9+
<p><code>callback</code> accepts the following arguments:</p>
10+
11+
<ul>
12+
<li><code>value</code> -&nbsp;represents the current element being processed in the array. It is the value of the element in the current iteration.</li>
13+
<li><code>index</code> -&nbsp;represents the index of the current element being processed in the array.</li>
14+
<li><code>array</code> -&nbsp;represents the array itself, allowing access to the entire array within the callback function.</li>
15+
</ul>
16+
17+
<p>The <code>context</code> is the object that should be passed as the function context parameter to the <code>callback</code> function, ensuring that the <code>this</code>&nbsp;keyword within the <code>callback</code> function refers to this <code>context</code> object.</p>
18+
19+
<p>Try to implement it without using the built-in array methods.</p>
20+
21+
<p>&nbsp;</p>
22+
<p><strong class="example">Example 1:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong>
26+
arr = [1,2,3],
27+
callback = (val, i, arr) =&gt; arr[i] = val * 2,
28+
context = {&quot;context&quot;:true}
29+
<strong>Output:</strong> [2,4,6]
30+
<strong>Explanation:</strong>
31+
arr.forEach(callback, context)&nbsp;
32+
console.log(arr) // [2,4,6]
33+
34+
The callback is executed on each element of the array.
35+
</pre>
36+
37+
<p><strong class="example">Example 2:</strong></p>
38+
39+
<pre>
40+
<strong>Input:</strong>
41+
arr = [true, true, false, false],
42+
callback = (val, i, arr) =&gt; arr[i] = this,
43+
context = {&quot;context&quot;: false}
44+
<strong>Output:</strong> [{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false}]
45+
<strong>Explanation:</strong>
46+
arr.forEach(callback, context)&nbsp;
47+
console.log(arr) // [{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false},{&quot;context&quot;:false}]
48+
49+
The callback is executed on each element of the array with the right context.
50+
</pre>
51+
52+
<p><strong class="example">Example 3:</strong></p>
53+
54+
<pre>
55+
<strong>Input:</strong>
56+
arr = [true, true, false, false],
57+
callback = (val, i, arr) =&gt; arr[i] = !val,
58+
context = {&quot;context&quot;: 5}
59+
<strong>Output:</strong> [false,false,true,true]
60+
</pre>
61+
62+
<p>&nbsp;</p>
63+
<p><strong>Constraints:</strong></p>
64+
65+
<ul>
66+
<li><code>arr</code> is a valid JSON array</li>
67+
<li><code>context</code> is a valid JSON object</li>
68+
<li><code>fn</code> is a function</li>
69+
<li><code>0 &lt;= arr.length &lt;= 10<sup>5</sup></code></li>
70+
</ul>
71+
72+
## Solutions
73+
74+
<!-- tabs:start -->
75+
76+
### **TypeScript**
77+
78+
```ts
79+
Array.prototype.forEach = function (callback: Function, context: any): void {
80+
for (let i = 0; i < this.length; ++i) {
81+
callback.call(context, this[i], i, this);
82+
}
83+
};
84+
85+
/**
86+
* const arr = [1,2,3];
87+
* const callback = (val, i, arr) => arr[i] = val * 2;
88+
* const context = {"context":true};
89+
*
90+
* arr.forEach(callback, context)
91+
*
92+
* console.log(arr) // [2,4,6]
93+
*/
94+
```
95+
96+
<!-- tabs:end -->

0 commit comments

Comments
 (0)