Skip to content

Commit 23e1dd1

Browse files
committed
feat: add solutions to lc problems: No.2618~2621
* No.2618.Check if Object Instance of Class * No.2619.Array Prototype Las * No.2620.Counter * No.2621.Sleep
1 parent 03b8096 commit 23e1dd1

File tree

12 files changed

+156
-8
lines changed

12 files changed

+156
-8
lines changed

solution/2600-2699/2618.Check if Object Instance of Class/README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,23 @@ Dog 是 Animal 的子类。因此,Dog 对象同时是 Dog 和 Animal 的实例
5858
<!-- 这里可写当前语言的特殊实现逻辑 -->
5959

6060
```ts
61-
61+
function checkIfInstanceOf(obj: any, classFunction: any): boolean {
62+
if (classFunction == null) {
63+
return false;
64+
}
65+
while (obj != null) {
66+
const proto = Object.getPrototypeOf(obj);
67+
if (proto === classFunction.prototype) {
68+
return true;
69+
}
70+
obj = proto;
71+
}
72+
return false;
73+
}
74+
75+
/**
76+
* checkIfInstanceOf(new Date(), Date); // true
77+
*/
6278
```
6379

6480
### **...**

solution/2600-2699/2618.Check if Object Instance of Class/README_EN.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,23 @@ Dog is a subclass of Animal. Therefore, a Dog object is an instance of both Dog
5252
### **TypeScript**
5353

5454
```ts
55-
55+
function checkIfInstanceOf(obj: any, classFunction: any): boolean {
56+
if (classFunction == null) {
57+
return false;
58+
}
59+
while (obj != null) {
60+
const proto = Object.getPrototypeOf(obj);
61+
if (proto === classFunction.prototype) {
62+
return true;
63+
}
64+
obj = proto;
65+
}
66+
return false;
67+
}
68+
69+
/**
70+
* checkIfInstanceOf(new Date(), Date); // true
71+
*/
5672
```
5773

5874
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function checkIfInstanceOf(obj: any, classFunction: any): boolean {
2+
if (classFunction == null) {
3+
return false;
4+
}
5+
while (obj != null) {
6+
const proto = Object.getPrototypeOf(obj);
7+
if (proto === classFunction.prototype) {
8+
return true;
9+
}
10+
obj = proto;
11+
}
12+
return false;
13+
}
14+
15+
/**
16+
* checkIfInstanceOf(new Date(), Date); // true
17+
*/

solution/2600-2699/2619.Array Prototype Last/README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,20 @@
4646
<!-- 这里可写当前语言的特殊实现逻辑 -->
4747

4848
```ts
49-
49+
declare global {
50+
interface Array<T> {
51+
last(): T | -1;
52+
}
53+
}
54+
55+
Array.prototype.last = function () {
56+
return this[this.length - 1] ?? -1;
57+
};
58+
59+
/**
60+
* const arr = [1, 2, 3];
61+
* arr.last(); // 3
62+
*/
5063
```
5164

5265
### **...**

solution/2600-2699/2619.Array Prototype Last/README_EN.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,20 @@ Write code that enhances all arrays such that you can call the&nbsp;<code>array.
3838
### **TypeScript**
3939

4040
```ts
41-
41+
declare global {
42+
interface Array<T> {
43+
last(): T | -1;
44+
}
45+
}
46+
47+
Array.prototype.last = function () {
48+
return this[this.length - 1] ?? -1;
49+
};
50+
51+
/**
52+
* const arr = [1, 2, 3];
53+
* arr.last(); // 3
54+
*/
4255
```
4356

4457
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
declare global {
2+
interface Array<T> {
3+
last(): T | -1;
4+
}
5+
}
6+
7+
Array.prototype.last = function () {
8+
return this[this.length - 1] ?? -1;
9+
};
10+
11+
/**
12+
* const arr = [1, 2, 3];
13+
* arr.last(); // 3
14+
*/

solution/2600-2699/2620.Counter/README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,19 @@ n = -2
5353
<!-- 这里可写当前语言的特殊实现逻辑 -->
5454

5555
```ts
56-
56+
function createCounter(n: number): () => number {
57+
let i = n;
58+
return function () {
59+
return i++;
60+
};
61+
}
62+
63+
/**
64+
* const counter = createCounter(10)
65+
* counter() // 10
66+
* counter() // 11
67+
* counter() // 12
68+
*/
5769
```
5870

5971
### **...**

solution/2600-2699/2620.Counter/README_EN.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,19 @@ n = -2
4545
### **TypeScript**
4646

4747
```ts
48-
48+
function createCounter(n: number): () => number {
49+
let i = n;
50+
return function () {
51+
return i++;
52+
};
53+
}
54+
55+
/**
56+
* const counter = createCounter(10)
57+
* counter() // 10
58+
* counter() // 11
59+
* counter() // 12
60+
*/
4961
```
5062

5163
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function createCounter(n: number): () => number {
2+
let i = n;
3+
return function () {
4+
return i++;
5+
};
6+
}
7+
8+
/**
9+
* const counter = createCounter(10)
10+
* counter() // 10
11+
* counter() // 11
12+
* counter() // 12
13+
*/

solution/2600-2699/2621.Sleep/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ sleep(100).then(() =&gt; {
5050
<!-- 这里可写当前语言的特殊实现逻辑 -->
5151

5252
```ts
53-
53+
async function sleep(millis: number): Promise<void> {
54+
return new Promise(r => setTimeout(r, millis));
55+
}
56+
57+
/**
58+
* let t = Date.now()
59+
* sleep(100).then(() => console.log(Date.now() - t)) // 100
60+
*/
5461
```
5562

5663
### **...**

solution/2600-2699/2621.Sleep/README_EN.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@ sleep(100).then(() =&gt; {
4141
### **TypeScript**
4242

4343
```ts
44-
44+
async function sleep(millis: number): Promise<void> {
45+
return new Promise(r => setTimeout(r, millis));
46+
}
47+
48+
/**
49+
* let t = Date.now()
50+
* sleep(100).then(() => console.log(Date.now() - t)) // 100
51+
*/
4552
```
4653

4754
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
async function sleep(millis: number): Promise<void> {
2+
return new Promise(r => setTimeout(r, millis));
3+
}
4+
5+
/**
6+
* let t = Date.now()
7+
* sleep(100).then(() => console.log(Date.now() - t)) // 100
8+
*/

0 commit comments

Comments
 (0)