Skip to content

Commit 2c81d40

Browse files
committed
feat: add solutions to lc problems: No.2634,2635
* No.2634.Filter Elements from Array * No.2635.Apply Transform Over Each Element in Array
1 parent b969f20 commit 2c81d40

File tree

6 files changed

+69
-4
lines changed

6 files changed

+69
-4
lines changed

solution/2600-2699/2634.Filter Elements from Array/README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,28 @@ const newArray = filter(arr, fn); // [20, 30]
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:遍历**
59+
60+
我们遍历数组 $arr$,对于每个元素 $arr[i]$,如果 $fn(arr[i], i)$ 为真,则将其加入答案数组中。最后返回答案数组即可。
61+
62+
时间复杂度 $O(n)$,其中 $n$ 为数组 $arr$ 的长度。忽略答案的空间消耗,空间复杂度 $O(1)$。
63+
5864
<!-- tabs:start -->
5965

6066
### **TypeScript**
6167

6268
<!-- 这里可写当前语言的特殊实现逻辑 -->
6369

6470
```ts
65-
71+
function filter(arr: number[], fn: (n: number, i: number) => any): number[] {
72+
const ans: number[] = [];
73+
for (let i = 0; i < arr.length; ++i) {
74+
if (fn(arr[i], i)) {
75+
ans.push(arr[i]);
76+
}
77+
}
78+
return ans;
79+
}
6680
```
6781

6882
### **...**

solution/2600-2699/2634.Filter Elements from Array/README_EN.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,26 @@ Falsey values such as 0 should be filtered out
4949

5050
## Solutions
5151

52+
**Approach 1: Traversal**
53+
54+
We traverse the array $arr$ and for each element $arr[i]$, if $fn(arr[i], i)$ is true, we add it to the answer array. Finally, we return the answer array.
55+
56+
The time complexity is $O(n)$, where $n$ is the length of the array $arr$. Ignoring the space consumption of the answer, the space complexity is $O(1)$.
57+
5258
<!-- tabs:start -->
5359

5460
### **TypeScript**
5561

5662
```ts
57-
63+
function filter(arr: number[], fn: (n: number, i: number) => any): number[] {
64+
const ans: number[] = [];
65+
for (let i = 0; i < arr.length; ++i) {
66+
if (fn(arr[i], i)) {
67+
ans.push(arr[i]);
68+
}
69+
}
70+
return ans;
71+
}
5872
```
5973

6074
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function filter(arr: number[], fn: (n: number, i: number) => any): number[] {
2+
const ans: number[] = [];
3+
for (let i = 0; i < arr.length; ++i) {
4+
if (fn(arr[i], i)) {
5+
ans.push(arr[i]);
6+
}
7+
}
8+
return ans;
9+
}

solution/2600-2699/2635.Apply Transform Over Each Element in Array/README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,25 @@ const newArray = map(arr, plusone); // [2,3,4]
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58+
**方法一:遍历**
59+
60+
我们遍历数组 $arr$,对于每个元素 $arr[i]$,将其替换为 $fn(arr[i], i)$。最后返回数组 $arr$ 即可。
61+
62+
时间复杂度 $O(n)$,其中 $n$ 为数组 $arr$ 的长度。空间复杂度 $O(1)$。
63+
5864
<!-- tabs:start -->
5965

6066
### **TypeScript**
6167

6268
<!-- 这里可写当前语言的特殊实现逻辑 -->
6369

6470
```ts
65-
71+
function map(arr: number[], fn: (n: number, i: number) => number): number[] {
72+
for (let i = 0; i < arr.length; ++i) {
73+
arr[i] = fn(arr[i], i);
74+
}
75+
return arr;
76+
}
6677
```
6778

6879
### **...**

solution/2600-2699/2635.Apply Transform Over Each Element in Array/README_EN.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,23 @@ The function increases each value in the array by one.
4848

4949
## Solutions
5050

51+
**Approach 1: traversal**
52+
53+
We traverse the array $arr$, for each element $arr[i]$, replace it with $fn(arr[i], i)$. Finally, return the array $arr$.
54+
55+
The time complexity is $O(n)$, where $n$ is the length of the array $arr$. The space complexity is $O(1)$.
56+
5157
<!-- tabs:start -->
5258

5359
### **TypeScript**
5460

5561
```ts
56-
62+
function map(arr: number[], fn: (n: number, i: number) => number): number[] {
63+
for (let i = 0; i < arr.length; ++i) {
64+
arr[i] = fn(arr[i], i);
65+
}
66+
return arr;
67+
}
5768
```
5869

5970
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function map(arr: number[], fn: (n: number, i: number) => number): number[] {
2+
for (let i = 0; i < arr.length; ++i) {
3+
arr[i] = fn(arr[i], i);
4+
}
5+
return arr;
6+
}

0 commit comments

Comments
 (0)