Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Oct 10, 2023
1 parent 4d0eae3 commit 315fbad
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 54 deletions.
27 changes: 17 additions & 10 deletions code/interview/实现大数相加.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
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);
let res = ''
let i = num1.length - 1
let j = num2.length - 1
let flag = 0
while(i >= 0 || j >= 0) {
let sum = flag;
if (i >= 0) sum += parseInt(num1[i--])
if (j >= 0) sum += parseInt(num2[j--])

flag = Math.floor(sum / 10)

res = sum % 10 + res;
}

if (flag > 0) {
res = flag + res;
}

return res;
};

Expand Down
85 changes: 85 additions & 0 deletions code/interview/实现并发控制.js
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);
10 changes: 9 additions & 1 deletion src/frontend-basic/js/0.1+0.2为什么不等于0.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ ECMAScript 中的 Number 类型使用 IEEE754 标准来表示整数和浮点数

ECMAScript 采用的就是双精确度,也就是说,会用 `64位` 来储存一个浮点数。

0.1的二进制是无限循环的,0.2的二进制也是无限循环的。所以0.3是不准确的
0.1的二进制是无限循环的,0.2的二进制也是无限循环的。所以0.3是不准确的


## 如何求小数二进制
0.625 * 2 = 1.25,所以第一位是1。
小数部分变为0.25。
0.25 * 2 = 0.5,所以第二位是0。
小数部分变为0.5。
0.5 * 2 = 1,所以第三位是1
110 changes: 79 additions & 31 deletions src/frontend-basic/js/手写题/实现Promise并发控制.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,93 @@

## 方式一
```js
async function asyncPool(poolLimit, array, iteratorFn) {
const ret = []; //2
const executing = []; //3
for (const item of array) { //4
const p = Promise.resolve().then(() => iteratorFn(item)); //5
ret.push(p); //6
if (poolLimit <= array.length) { //7
const e = p.then(() => executing.splice(executing.indexOf(e), 1)); //8
executing.push(e); //9
if (executing.length >= poolLimit) { //10
await Promise.race(executing); //11
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)
if (exec.length >= limit) {
await Promise.race(exec)
}
}
}
return Promise.all(ret); //15

return Promise.all(res)
}
```


```js
const curl = (i) => {
console.log('开始' + i);
return new Promise((resolve) => setTimeout(() => {
resolve(i);
console.log('结束' + i);
}, 1000+Math.random()*1000));
};

/*
const curl = (i) => {
console.log('开始' + i);
return i;
};
*/
let urls = Array(10).fill(0).map((v,i) => i);
(async () => {
const res = await asyncPool(3, urls, curl);
console.log(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);
```

## 方式二
Expand Down
40 changes: 28 additions & 12 deletions src/frontend-basic/js/手写题/实现大数相加.md
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'
```

0 comments on commit 315fbad

Please sign in to comment.