-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d0eae3
commit 315fbad
Showing
5 changed files
with
218 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
async function asyncPool(limit, fns) { | ||
const res = [] | ||
const exec = [] | ||
|
||
for (const item of fns) { | ||
const p = Promise.resolve().then(() => item()) | ||
|
||
res.push(p) | ||
|
||
if (limit <= fns.length) { | ||
const execute = p.then(() => { | ||
exec.splice(exec.indexOf(execute), 1) | ||
}) | ||
exec.push(p) | ||
console.log(exec.length) | ||
if (exec.length >= limit) { | ||
console.log('达到并发数量', limit) | ||
await Promise.race(exec) | ||
} | ||
} | ||
} | ||
|
||
return Promise.all(res) | ||
} | ||
|
||
// 示例测试用例 | ||
async function runTest() { | ||
const taskLimit = 3; // 最大并发数 | ||
const tasks = [ | ||
async () => { | ||
await sleep(1000); // 模拟异步任务 | ||
console.log('执行完成 1'); | ||
return 1; | ||
}, | ||
async () => { | ||
await sleep(1000); // 模拟异步任务 | ||
console.log('执行完成 2'); | ||
return 2; | ||
}, | ||
async () => { | ||
await sleep(1000); // 模拟异步任务 | ||
console.log('执行完成 3'); | ||
return 3; | ||
}, | ||
async () => { | ||
await sleep(500); // 模拟异步任务 | ||
console.log('执行完成 4'); | ||
return 4; | ||
}, | ||
async () => { | ||
await sleep(2000); // 模拟异步任务 | ||
console.log('执行完成 5'); | ||
return 5; | ||
}, | ||
async () => { | ||
await sleep(1000); // 模拟异步任务 | ||
console.log('执行完成 6'); | ||
return 6; | ||
}, | ||
async () => { | ||
await sleep(3000); // 模拟异步任务 | ||
console.log('执行完成 7'); | ||
return 7; | ||
}, | ||
async () => { | ||
await sleep(500); // 模拟异步任务 | ||
console.log('执行完成 8'); | ||
return 8; | ||
}, | ||
]; | ||
|
||
console.log(`Running asyncPool with a limit of ${taskLimit}`); | ||
const results = await asyncPool(taskLimit, tasks); | ||
console.log(`Results: ${results}`); | ||
} | ||
|
||
// 辅助函数,模拟异步任务 | ||
function sleep(ms) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
|
||
// 运行测试用例 | ||
runTest().then(() => { | ||
console.log('执行完毕'); | ||
}).catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,34 @@ | ||
# 实现大数相加 | ||
|
||
```js | ||
const addStrings = (num1, num2) => { | ||
let res = ""; | ||
let i = num1.length - 1; | ||
let j = num2.length - 1; | ||
let flag = 0; | ||
while (i >= 0 || j >= 0 || flag !== 0) { | ||
if (i >= 0) flag += parseInt(num1[i--]); | ||
if (j >= 0) flag += parseInt(num2[j--]); | ||
// 取余 | ||
res = (flag % 10) + res; | ||
flag = Math.floor(flag / 10); | ||
const addStrings = (a, b) => { | ||
let res = ''; | ||
let i = a.length - 1; | ||
let j = b.length - 1; | ||
let flag = 0 | ||
while(i >= 0 || j >= 0) { | ||
let sum = 0; | ||
if (i >= 0) sum += parseInt(a[i--]) | ||
if (j >= 0) sum += parseInt(b[j--]) | ||
// 加上上次的进位 | ||
sum += flag; | ||
|
||
// 更新进位 | ||
flag = Math.floor(sum / 10) | ||
|
||
// 计算值 | ||
res = sum % 10 + res; | ||
} | ||
|
||
// 说明第一位相加 还是有进位 | ||
if (flag > 0) { | ||
res = flag + res; | ||
} | ||
return res; | ||
}; | ||
} | ||
|
||
console.log(addStrings('1234', '2345')); // 输出 '3589' | ||
console.log(addStrings('987654321098765432109876543210', '123456789012345678901234567890')); // 输出 '1111111110111111110111111111000' | ||
console.log(addStrings('999999', '1')); // 输出 '1000000' | ||
console.log(addStrings('0', '0')); // 输出 '0' | ||
``` |