Skip to content

Commit 7e5035e

Browse files
committed
feat: add solutions to lc problems: No.2700,2703
1 parent f5929c6 commit 7e5035e

File tree

6 files changed

+74
-0
lines changed

6 files changed

+74
-0
lines changed

solution/2700-2799/2700.Differences Between Two Objects/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,25 @@ obj2 = {  
137137
<!-- 这里可写当前语言的特殊实现逻辑 -->
138138

139139
```ts
140+
function objDiff(obj1: any, obj2: any): any {
141+
if (type(obj1) !== type(obj2)) return [obj1, obj2];
142+
if (!isObject(obj1)) return obj1 === obj2 ? {} : [obj1, obj2];
143+
const diff: Record<string, unknown> = {};
144+
const sameKeys = Object.keys(obj1).filter(key => key in obj2);
145+
sameKeys.forEach(key => {
146+
const subDiff = objDiff(obj1[key], obj2[key]);
147+
if (Object.keys(subDiff).length) diff[key] = subDiff;
148+
});
149+
return diff;
150+
}
151+
152+
function type(obj: unknown): string {
153+
return Object.prototype.toString.call(obj).slice(8, -1);
154+
}
140155

156+
function isObject(obj: unknown): obj is Record<string, unknown> {
157+
return typeof obj === 'object' && obj !== null;
158+
}
141159
```
142160

143161
<!-- tabs:end -->

solution/2700-2799/2700.Differences Between Two Objects/README_EN.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,25 @@ obj2 = { &nbsp;
129129
### **TypeScript**
130130

131131
```ts
132+
function objDiff(obj1: any, obj2: any): any {
133+
if (type(obj1) !== type(obj2)) return [obj1, obj2];
134+
if (!isObject(obj1)) return obj1 === obj2 ? {} : [obj1, obj2];
135+
const diff: Record<string, unknown> = {};
136+
const sameKeys = Object.keys(obj1).filter(key => key in obj2);
137+
sameKeys.forEach(key => {
138+
const subDiff = objDiff(obj1[key], obj2[key]);
139+
if (Object.keys(subDiff).length) diff[key] = subDiff;
140+
});
141+
return diff;
142+
}
143+
144+
function type(obj: unknown): string {
145+
return Object.prototype.toString.call(obj).slice(8, -1);
146+
}
132147

148+
function isObject(obj: unknown): obj is Record<string, unknown> {
149+
return typeof obj === 'object' && obj !== null;
150+
}
133151
```
134152

135153
<!-- tabs:end -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function objDiff(obj1: any, obj2: any): any {
2+
if (type(obj1) !== type(obj2)) return [obj1, obj2];
3+
if (!isObject(obj1)) return obj1 === obj2 ? {} : [obj1, obj2];
4+
const diff: Record<string, unknown> = {};
5+
const sameKeys = Object.keys(obj1).filter(key => key in obj2);
6+
sameKeys.forEach(key => {
7+
const subDiff = objDiff(obj1[key], obj2[key]);
8+
if (Object.keys(subDiff).length) diff[key] = subDiff;
9+
});
10+
return diff;
11+
}
12+
13+
function type(obj: unknown): string {
14+
return Object.prototype.toString.call(obj).slice(8, -1);
15+
}
16+
17+
function isObject(obj: unknown): obj is Record<string, unknown> {
18+
return typeof obj === 'object' && obj !== null;
19+
}

solution/2700-2799/2703.Return Length of Arguments Passed/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ Three values were passed to the function so it should return 3.
5050
<!-- 这里可写当前语言的特殊实现逻辑 -->
5151

5252
```ts
53+
function argumentsLength(...args: any[]): number {
54+
return args.length;
55+
}
5356

57+
/**
58+
* argumentsLength(1, 2, 3); // 3
59+
*/
5460
```
5561

5662
<!-- tabs:end -->

solution/2700-2799/2703.Return Length of Arguments Passed/README_EN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ Three values were passed to the function so it should return 3.
4444
### **TypeScript**
4545

4646
```ts
47+
function argumentsLength(...args: any[]): number {
48+
return args.length;
49+
}
4750

51+
/**
52+
* argumentsLength(1, 2, 3); // 3
53+
*/
4854
```
4955

5056
<!-- tabs:end -->
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function argumentsLength(...args: any[]): number {
2+
return args.length;
3+
}
4+
5+
/**
6+
* argumentsLength(1, 2, 3); // 3
7+
*/

0 commit comments

Comments
 (0)