Skip to content

Commit 433384a

Browse files
committed
feat: add solutions to lc problems: No.2694,2695
* No.2694.Event Emitter * No.2695.Array Wrapper
1 parent e0c9f41 commit 433384a

File tree

6 files changed

+195
-4
lines changed

6 files changed

+195
-4
lines changed

solution/2600-2699/2694.Event Emitter/README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,46 @@ emitter.emit("firstEvent", [4, 5, 6]); // [], there are no subscriptio
8282
<!-- 这里可写当前语言的特殊实现逻辑 -->
8383

8484
```ts
85-
85+
type Callback = (...args: any[]) => any;
86+
type Subscription = {
87+
unsubscribe: () => void;
88+
};
89+
90+
class EventEmitter {
91+
private d: Map<string, Set<Callback>> = new Map();
92+
93+
subscribe(eventName: string, callback: Callback): Subscription {
94+
this.d.set(
95+
eventName,
96+
(this.d.get(eventName) || new Set()).add(callback),
97+
);
98+
return {
99+
unsubscribe: () => {
100+
this.d.get(eventName)?.delete(callback);
101+
},
102+
};
103+
}
104+
105+
emit(eventName: string, args: any[] = []): any {
106+
const callbacks = this.d.get(eventName);
107+
if (!callbacks) {
108+
return [];
109+
}
110+
return [...callbacks].map(callback => callback(...args));
111+
}
112+
}
113+
114+
/**
115+
* const emitter = new EventEmitter();
116+
*
117+
* // Subscribe to the onClick event with onClickCallback
118+
* function onClickCallback() { return 99 }
119+
* const sub = emitter.subscribe('onClick', onClickCallback);
120+
*
121+
* emitter.emit('onClick'); // [99]
122+
* sub.unsubscribe(); // undefined
123+
* emitter.emit('onClick'); // []
124+
*/
86125
```
87126

88127
<!-- tabs:end -->

solution/2600-2699/2694.Event Emitter/README_EN.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,46 @@ emitter.emit(&quot;firstEvent&quot;, [4, 5, 6]); // [], there are no subscriptio
7676
### **TypeScript**
7777

7878
```ts
79-
79+
type Callback = (...args: any[]) => any;
80+
type Subscription = {
81+
unsubscribe: () => void;
82+
};
83+
84+
class EventEmitter {
85+
private d: Map<string, Set<Callback>> = new Map();
86+
87+
subscribe(eventName: string, callback: Callback): Subscription {
88+
this.d.set(
89+
eventName,
90+
(this.d.get(eventName) || new Set()).add(callback),
91+
);
92+
return {
93+
unsubscribe: () => {
94+
this.d.get(eventName)?.delete(callback);
95+
},
96+
};
97+
}
98+
99+
emit(eventName: string, args: any[] = []): any {
100+
const callbacks = this.d.get(eventName);
101+
if (!callbacks) {
102+
return [];
103+
}
104+
return [...callbacks].map(callback => callback(...args));
105+
}
106+
}
107+
108+
/**
109+
* const emitter = new EventEmitter();
110+
*
111+
* // Subscribe to the onClick event with onClickCallback
112+
* function onClickCallback() { return 99 }
113+
* const sub = emitter.subscribe('onClick', onClickCallback);
114+
*
115+
* emitter.emit('onClick'); // [99]
116+
* sub.unsubscribe(); // undefined
117+
* emitter.emit('onClick'); // []
118+
*/
80119
```
81120

82121
<!-- tabs:end -->
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
type Callback = (...args: any[]) => any;
2+
type Subscription = {
3+
unsubscribe: () => void;
4+
};
5+
6+
class EventEmitter {
7+
private d: Map<string, Set<Callback>> = new Map();
8+
9+
subscribe(eventName: string, callback: Callback): Subscription {
10+
this.d.set(
11+
eventName,
12+
(this.d.get(eventName) || new Set()).add(callback),
13+
);
14+
return {
15+
unsubscribe: () => {
16+
this.d.get(eventName)?.delete(callback);
17+
},
18+
};
19+
}
20+
21+
emit(eventName: string, args: any[] = []): any {
22+
const callbacks = this.d.get(eventName);
23+
if (!callbacks) {
24+
return [];
25+
}
26+
return [...callbacks].map(callback => callback(...args));
27+
}
28+
}
29+
30+
/**
31+
* const emitter = new EventEmitter();
32+
*
33+
* // Subscribe to the onClick event with onClickCallback
34+
* function onClickCallback() { return 99 }
35+
* const sub = emitter.subscribe('onClick', onClickCallback);
36+
*
37+
* emitter.emit('onClick'); // [99]
38+
* sub.unsubscribe(); // undefined
39+
* emitter.emit('onClick'); // []
40+
*/

solution/2600-2699/2695.Array Wrapper/README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,31 @@ obj1 + obj2; // 0
6666
<!-- 这里可写当前语言的特殊实现逻辑 -->
6767

6868
```ts
69-
69+
class ArrayWrapper {
70+
private nums: number[];
71+
private s: number;
72+
73+
constructor(nums: number[]) {
74+
this.nums = nums;
75+
this.s = nums.reduce((a, b) => a + b, 0);
76+
}
77+
78+
valueOf() {
79+
return this.s;
80+
}
81+
82+
toString() {
83+
return `[${this.nums.join(',')}]`;
84+
}
85+
}
86+
87+
/**
88+
* const obj1 = new ArrayWrapper([1,2]);
89+
* const obj2 = new ArrayWrapper([3,4]);
90+
* obj1 + obj2; // 10
91+
* String(obj1); // "[1,2]"
92+
* String(obj2); // "[3,4]"
93+
*/
7094
```
7195

7296
<!-- tabs:end -->

solution/2600-2699/2695.Array Wrapper/README_EN.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,31 @@ obj1 + obj2; // 0
6060
### **TypeScript**
6161

6262
```ts
63-
63+
class ArrayWrapper {
64+
private nums: number[];
65+
private s: number;
66+
67+
constructor(nums: number[]) {
68+
this.nums = nums;
69+
this.s = nums.reduce((a, b) => a + b, 0);
70+
}
71+
72+
valueOf() {
73+
return this.s;
74+
}
75+
76+
toString() {
77+
return `[${this.nums.join(',')}]`;
78+
}
79+
}
80+
81+
/**
82+
* const obj1 = new ArrayWrapper([1,2]);
83+
* const obj2 = new ArrayWrapper([3,4]);
84+
* obj1 + obj2; // 10
85+
* String(obj1); // "[1,2]"
86+
* String(obj2); // "[3,4]"
87+
*/
6488
```
6589

6690
<!-- tabs:end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class ArrayWrapper {
2+
private nums: number[];
3+
private s: number;
4+
5+
constructor(nums: number[]) {
6+
this.nums = nums;
7+
this.s = nums.reduce((a, b) => a + b, 0);
8+
}
9+
10+
valueOf() {
11+
return this.s;
12+
}
13+
14+
toString() {
15+
return `[${this.nums.join(',')}]`;
16+
}
17+
}
18+
19+
/**
20+
* const obj1 = new ArrayWrapper([1,2]);
21+
* const obj2 = new ArrayWrapper([3,4]);
22+
* obj1 + obj2; // 10
23+
* String(obj1); // "[1,2]"
24+
* String(obj2); // "[3,4]"
25+
*/

0 commit comments

Comments
 (0)