Skip to content

Commit 3a6772c

Browse files
committed
feat: add solutions to lc problems: No.2628,2629
* No.2628.JSON Deep Equal * No.2629.Function Composition
1 parent 62f11da commit 3a6772c

File tree

6 files changed

+152
-4
lines changed

6 files changed

+152
-4
lines changed

solution/2600-2699/2628.JSON Deep Equal/README.md

+48-1
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,61 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
**方法一:递归**
65+
66+
我们先判断 `o1` 是否为空,或者 `o1` 是否非对象类型。如果是,则直接返回 `o1``o2` 是否相等。
67+
68+
否则,我们判断 `o1``o2` 的类型是否相同。如果不同,则返回 `false`
69+
70+
接下来,我们判断 `o1``o2` 是否都是数组。如果不是,则返回 `false`
71+
72+
如果是数组,则判断两个数组的长度是否相同。如果不同,则返回 `false`。否则,我们遍历两个数组对应位置的元素,递归调用 `areDeeplyEqual` 函数,判断两个元素是否相等。如果有一个元素不相等,则返回 `false`。否则,返回 `true`
73+
74+
如果 `o1``o2` 都不是数组,则判断两个对象的键的个数是否相同。如果不同,则返回 `false`。否则,我们遍历 `o1` 的所有键,递归调用 `areDeeplyEqual` 函数,判断两个键对应的值是否相等。如果有一个键对应的值不相等,则返回 `false`。否则,返回 `true`
75+
76+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 `o1``o2` 的长度。
77+
6478
<!-- tabs:start -->
6579

6680
### **TypeScript**
6781

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

7084
```ts
71-
85+
function areDeeplyEqual(o1: any, o2: any): boolean {
86+
if (o1 === null || typeof o1 !== 'object') {
87+
return o1 === o2;
88+
}
89+
if (typeof o1 !== typeof o2) {
90+
return false;
91+
}
92+
if (Array.isArray(o1) !== Array.isArray(o2)) {
93+
return false;
94+
}
95+
if (Array.isArray(o1)) {
96+
if (o1.length !== o2.length) {
97+
return false;
98+
}
99+
for (let i = 0; i < o1.length; i++) {
100+
if (!areDeeplyEqual(o1[i], o2[i])) {
101+
return false;
102+
}
103+
}
104+
return true;
105+
} else {
106+
const keys1 = Object.keys(o1);
107+
const keys2 = Object.keys(o2);
108+
if (keys1.length !== keys2.length) {
109+
return false;
110+
}
111+
for (const key of keys1) {
112+
if (!areDeeplyEqual(o1[key], o2[key])) {
113+
return false;
114+
}
115+
}
116+
return true;
117+
}
118+
}
72119
```
73120

74121
### **...**

solution/2600-2699/2628.JSON Deep Equal/README_EN.md

+34-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,40 @@
6060
### **TypeScript**
6161

6262
```ts
63-
63+
function areDeeplyEqual(o1: any, o2: any): boolean {
64+
if (o1 === null || typeof o1 !== 'object') {
65+
return o1 === o2;
66+
}
67+
if (typeof o1 !== typeof o2) {
68+
return false;
69+
}
70+
if (Array.isArray(o1) !== Array.isArray(o2)) {
71+
return false;
72+
}
73+
if (Array.isArray(o1)) {
74+
if (o1.length !== o2.length) {
75+
return false;
76+
}
77+
for (let i = 0; i < o1.length; i++) {
78+
if (!areDeeplyEqual(o1[i], o2[i])) {
79+
return false;
80+
}
81+
}
82+
return true;
83+
} else {
84+
const keys1 = Object.keys(o1);
85+
const keys2 = Object.keys(o2);
86+
if (keys1.length !== keys2.length) {
87+
return false;
88+
}
89+
for (const key of keys1) {
90+
if (!areDeeplyEqual(o1[key], o2[key])) {
91+
return false;
92+
}
93+
}
94+
return true;
95+
}
96+
}
6497
```
6598

6699
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function areDeeplyEqual(o1: any, o2: any): boolean {
2+
if (o1 === null || typeof o1 !== 'object') {
3+
return o1 === o2;
4+
}
5+
if (typeof o1 !== typeof o2) {
6+
return false;
7+
}
8+
if (Array.isArray(o1) !== Array.isArray(o2)) {
9+
return false;
10+
}
11+
if (Array.isArray(o1)) {
12+
if (o1.length !== o2.length) {
13+
return false;
14+
}
15+
for (let i = 0; i < o1.length; i++) {
16+
if (!areDeeplyEqual(o1[i], o2[i])) {
17+
return false;
18+
}
19+
}
20+
return true;
21+
} else {
22+
const keys1 = Object.keys(o1);
23+
const keys2 = Object.keys(o2);
24+
if (keys1.length !== keys2.length) {
25+
return false;
26+
}
27+
for (const key of keys1) {
28+
if (!areDeeplyEqual(o1[key], o2[key])) {
29+
return false;
30+
}
31+
}
32+
return true;
33+
}
34+
}

solution/2600-2699/2629.Function Composition/README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,18 @@ Starting with x = 4.
7070
<!-- 这里可写当前语言的特殊实现逻辑 -->
7171

7272
```ts
73-
73+
type F = (x: number) => number;
74+
75+
function compose(functions: F[]): F {
76+
return function (x) {
77+
return functions.reduceRight((acc, fn) => fn(acc), x);
78+
};
79+
}
80+
81+
/**
82+
* const fn = compose([x => x + 1, x => 2 * x])
83+
* fn(4) // 9
84+
*/
7485
```
7586

7687
### **...**

solution/2600-2699/2629.Function Composition/README_EN.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,18 @@ The composition of zero functions is the identity function</pre>
6262
### **TypeScript**
6363

6464
```ts
65-
65+
type F = (x: number) => number;
66+
67+
function compose(functions: F[]): F {
68+
return function (x) {
69+
return functions.reduceRight((acc, fn) => fn(acc), x);
70+
};
71+
}
72+
73+
/**
74+
* const fn = compose([x => x + 1, x => 2 * x])
75+
* fn(4) // 9
76+
*/
6677
```
6778

6879
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
type F = (x: number) => number;
2+
3+
function compose(functions: F[]): F {
4+
return function (x) {
5+
return functions.reduceRight((acc, fn) => fn(acc), x);
6+
};
7+
}
8+
9+
/**
10+
* const fn = compose([x => x + 1, x => 2 * x])
11+
* fn(4) // 9
12+
*/

0 commit comments

Comments
 (0)