Skip to content

Commit d14beae

Browse files
authored
feat: add solutions to lc problems: No.2794~2797 (doocs#1336)
* No.2794.Create Object from Two Arrays * No.2795.Parallel Execution of Promises for Individual Results Retrieval * No.2796.Repeat String * No.2797.Partial Function with Placeholders
1 parent aff2030 commit d14beae

File tree

16 files changed

+428
-0
lines changed

16 files changed

+428
-0
lines changed

solution/2700-2799/2794.Create Object from Two Arrays/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,36 @@
6060
<!-- 这里可写当前语言的特殊实现逻辑 -->
6161

6262
```ts
63+
function createObject(keysArr: any[], valuesArr: any[]): Record<string, any> {
64+
const ans: Record<string, any> = {};
65+
for (let i = 0; i < keysArr.length; ++i) {
66+
const k = String(keysArr[i]);
67+
if (ans[k] === undefined) {
68+
ans[k] = valuesArr[i];
69+
}
70+
}
71+
return ans;
72+
}
73+
```
6374

75+
### **JavaScript**
76+
77+
```js
78+
/**
79+
* @param {Array} keysArr
80+
* @param {Array} valuesArr
81+
* @return {Object}
82+
*/
83+
var createObject = function (keysArr, valuesArr) {
84+
const ans = {};
85+
for (let i = 0; i < keysArr.length; ++i) {
86+
const k = keysArr[i] + '';
87+
if (ans[k] === undefined) {
88+
ans[k] = valuesArr[i];
89+
}
90+
}
91+
return ans;
92+
};
6493
```
6594

6695
<!-- tabs:end -->

solution/2700-2799/2794.Create Object from Two Arrays/README_EN.md

+29
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,36 @@
5252
### **TypeScript**
5353

5454
```ts
55+
function createObject(keysArr: any[], valuesArr: any[]): Record<string, any> {
56+
const ans: Record<string, any> = {};
57+
for (let i = 0; i < keysArr.length; ++i) {
58+
const k = String(keysArr[i]);
59+
if (ans[k] === undefined) {
60+
ans[k] = valuesArr[i];
61+
}
62+
}
63+
return ans;
64+
}
65+
```
5566

67+
### **JavaScript**
68+
69+
```js
70+
/**
71+
* @param {Array} keysArr
72+
* @param {Array} valuesArr
73+
* @return {Object}
74+
*/
75+
var createObject = function (keysArr, valuesArr) {
76+
const ans = {};
77+
for (let i = 0; i < keysArr.length; ++i) {
78+
const k = keysArr[i] + '';
79+
if (ans[k] === undefined) {
80+
ans[k] = valuesArr[i];
81+
}
82+
}
83+
return ans;
84+
};
5685
```
5786

5887
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {Array} keysArr
3+
* @param {Array} valuesArr
4+
* @return {Object}
5+
*/
6+
var createObject = function (keysArr, valuesArr) {
7+
const ans = {};
8+
for (let i = 0; i < keysArr.length; ++i) {
9+
const k = keysArr[i] + '';
10+
if (ans[k] === undefined) {
11+
ans[k] = valuesArr[i];
12+
}
13+
}
14+
return ans;
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function createObject(keysArr: any[], valuesArr: any[]): Record<string, any> {
2+
const ans: Record<string, any> = {};
3+
for (let i = 0; i < keysArr.length; ++i) {
4+
const k = String(keysArr[i]);
5+
if (ans[k] === undefined) {
6+
ans[k] = valuesArr[i];
7+
}
8+
}
9+
return ans;
10+
}

solution/2700-2799/2795.Parallel Execution of Promises for Individual Results Retrieval/README.md

+66
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,73 @@ promise.then(res =&gt; {
9696
<!-- 这里可写当前语言的特殊实现逻辑 -->
9797

9898
```ts
99+
type FulfilledObj = {
100+
status: 'fulfilled';
101+
value: string;
102+
};
103+
type RejectedObj = {
104+
status: 'rejected';
105+
reason: string;
106+
};
107+
type Obj = FulfilledObj | RejectedObj;
108+
109+
function promiseAllSettled(functions: Function[]): Promise<Obj[]> {
110+
return new Promise(resolve => {
111+
const res: Obj[] = [];
112+
let count = 0;
113+
for (let i in functions) {
114+
functions[i]()
115+
.then(value => ({ status: 'fulfilled', value }))
116+
.catch(reason => ({ status: 'rejected', reason }))
117+
.then(obj => {
118+
res[i] = obj;
119+
if (++count === functions.length) {
120+
resolve(res);
121+
}
122+
});
123+
}
124+
});
125+
}
126+
127+
/**
128+
* const functions = [
129+
* () => new Promise(resolve => setTimeout(() => resolve(15), 100))
130+
* ]
131+
* const time = performance.now()
132+
*
133+
* const promise = promiseAllSettled(functions);
134+
*
135+
* promise.then(res => {
136+
* const out = {t: Math.floor(performance.now() - time), values: res}
137+
* console.log(out) // {"t":100,"values":[{"status":"fulfilled","value":15}]}
138+
* })
139+
*/
140+
```
99141

142+
### **JavaScript**
143+
144+
```js
145+
/**
146+
* @param {Array<Function>} functions
147+
* @return {Promise}
148+
*/
149+
var promiseAllSettled = function (functions) {
150+
return new Promise(resolve => {
151+
const res = [];
152+
let count = 0;
153+
for (let i in functions) {
154+
functions[i]()
155+
.then(value => ({ status: 'fulfilled', value }))
156+
.catch(reason => ({ status: 'rejected', reason }))
157+
.then(obj => {
158+
res[i] = obj;
159+
if (++count === functions.length) {
160+
resolve(res);
161+
}
162+
});
163+
}
164+
});
165+
};
100166
```
101167

102168
<!-- tabs:end -->

solution/2700-2799/2795.Parallel Execution of Promises for Individual Results Retrieval/README_EN.md

+66
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,73 @@ The returned promise resolves within 100 milliseconds. Since promise from the ar
8888
### **TypeScript**
8989

9090
```ts
91+
type FulfilledObj = {
92+
status: 'fulfilled';
93+
value: string;
94+
};
95+
type RejectedObj = {
96+
status: 'rejected';
97+
reason: string;
98+
};
99+
type Obj = FulfilledObj | RejectedObj;
100+
101+
function promiseAllSettled(functions: Function[]): Promise<Obj[]> {
102+
return new Promise(resolve => {
103+
const res: Obj[] = [];
104+
let count = 0;
105+
for (let i in functions) {
106+
functions[i]()
107+
.then(value => ({ status: 'fulfilled', value }))
108+
.catch(reason => ({ status: 'rejected', reason }))
109+
.then(obj => {
110+
res[i] = obj;
111+
if (++count === functions.length) {
112+
resolve(res);
113+
}
114+
});
115+
}
116+
});
117+
}
118+
119+
/**
120+
* const functions = [
121+
* () => new Promise(resolve => setTimeout(() => resolve(15), 100))
122+
* ]
123+
* const time = performance.now()
124+
*
125+
* const promise = promiseAllSettled(functions);
126+
*
127+
* promise.then(res => {
128+
* const out = {t: Math.floor(performance.now() - time), values: res}
129+
* console.log(out) // {"t":100,"values":[{"status":"fulfilled","value":15}]}
130+
* })
131+
*/
132+
```
91133

134+
### **JavaScript**
135+
136+
```js
137+
/**
138+
* @param {Array<Function>} functions
139+
* @return {Promise}
140+
*/
141+
var promiseAllSettled = function (functions) {
142+
return new Promise(resolve => {
143+
const res = [];
144+
let count = 0;
145+
for (let i in functions) {
146+
functions[i]()
147+
.then(value => ({ status: 'fulfilled', value }))
148+
.catch(reason => ({ status: 'rejected', reason }))
149+
.then(obj => {
150+
res[i] = obj;
151+
if (++count === functions.length) {
152+
resolve(res);
153+
}
154+
});
155+
}
156+
});
157+
};
92158
```
93159

94160
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {Array<Function>} functions
3+
* @return {Promise}
4+
*/
5+
var promiseAllSettled = function (functions) {
6+
return new Promise(resolve => {
7+
const res = [];
8+
let count = 0;
9+
for (let i in functions) {
10+
functions[i]()
11+
.then(value => ({ status: 'fulfilled', value }))
12+
.catch(reason => ({ status: 'rejected', reason }))
13+
.then(obj => {
14+
res[i] = obj;
15+
if (++count === functions.length) {
16+
resolve(res);
17+
}
18+
});
19+
}
20+
});
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
type FulfilledObj = {
2+
status: 'fulfilled';
3+
value: string;
4+
};
5+
type RejectedObj = {
6+
status: 'rejected';
7+
reason: string;
8+
};
9+
type Obj = FulfilledObj | RejectedObj;
10+
11+
function promiseAllSettled(functions: Function[]): Promise<Obj[]> {
12+
return new Promise(resolve => {
13+
const res: Obj[] = [];
14+
let count = 0;
15+
for (let i in functions) {
16+
functions[i]()
17+
.then(value => ({ status: 'fulfilled', value }))
18+
.catch(reason => ({ status: 'rejected', reason }))
19+
.then(obj => {
20+
res[i] = obj;
21+
if (++count === functions.length) {
22+
resolve(res);
23+
}
24+
});
25+
}
26+
});
27+
}
28+
29+
/**
30+
* const functions = [
31+
* () => new Promise(resolve => setTimeout(() => resolve(15), 100))
32+
* ]
33+
* const time = performance.now()
34+
*
35+
* const promise = promiseAllSettled(functions);
36+
*
37+
* promise.then(res => {
38+
* const out = {t: Math.floor(performance.now() - time), values: res}
39+
* console.log(out) // {"t":100,"values":[{"status":"fulfilled","value":15}]}
40+
* })
41+
*/

solution/2700-2799/2796.Repeat String/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,23 @@
5656
<!-- 这里可写当前语言的特殊实现逻辑 -->
5757

5858
```ts
59+
declare global {
60+
interface String {
61+
replicate(times: number): string;
62+
}
63+
}
64+
65+
String.prototype.replicate = function (times: number) {
66+
return new Array(times).fill(this).join('');
67+
};
68+
```
69+
70+
### **JavaScript**
5971

72+
```js
73+
String.prototype.replicate = function (times) {
74+
return Array(times).fill(this).join('');
75+
};
6076
```
6177

6278
<!-- tabs:end -->

solution/2700-2799/2796.Repeat String/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,23 @@
4848
### **TypeScript**
4949

5050
```ts
51+
declare global {
52+
interface String {
53+
replicate(times: number): string;
54+
}
55+
}
56+
57+
String.prototype.replicate = function (times: number) {
58+
return new Array(times).fill(this).join('');
59+
};
60+
```
61+
62+
### **JavaScript**
5163

64+
```js
65+
String.prototype.replicate = function (times) {
66+
return Array(times).fill(this).join('');
67+
};
5268
```
5369

5470
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
String.prototype.replicate = function (times) {
2+
return Array(times).fill(this).join('');
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
declare global {
2+
interface String {
3+
replicate(times: number): string;
4+
}
5+
}
6+
7+
String.prototype.replicate = function (times: number) {
8+
return new Array(times).fill(this).join('');
9+
};

0 commit comments

Comments
 (0)