-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
2 changed files
with
59 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,38 @@ | ||
/* | ||
* @Author: haigeno1 [email protected] | ||
* @Date: 2022-06-26 17:41:23 | ||
* @LastEditors: Please set LastEditors | ||
* @LastEditTime: 2022-07-21 12:28:25 | ||
* @FilePath: /js-snippets/test.js | ||
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE | ||
*/ | ||
const change = (amount, coins) => { | ||
let dp = Array(amount + 1).fill(0); | ||
console.log('1'); // 1 | ||
|
||
setTimeout(function () { | ||
console.log('2'); // 5 | ||
process.nextTick(function () { | ||
console.log('3'); // 7 | ||
}) | ||
new Promise(function (resolve) { | ||
console.log('4'); // 6 | ||
resolve(); | ||
}).then(function () { | ||
console.log('5') // 8 | ||
}) | ||
}) | ||
|
||
|
||
dp[0] = 1; | ||
process.nextTick(function () { | ||
console.log('6'); // 3 | ||
}) | ||
|
||
new Promise(function (resolve) { | ||
console.log('7'); // 2 | ||
resolve(); | ||
}).then(function () { | ||
console.log('8') // 4 | ||
}) | ||
|
||
let res = Array(amount + 1).fill(0).map(() => []) | ||
|
||
for (let j = 0; j <= amount; j++) { | ||
for (let i = 0; i < coins.length; i++) { | ||
if (j >= coins[i]) { | ||
dp[j] += dp[j - coins[i]]; | ||
console.log(i, j, coins[i], dp); | ||
|
||
let toBeAdd | ||
if (j - coins[i] > 0) { | ||
toBeAdd = res[j - coins[i]].map(it => [...it, coins[i]]) | ||
} else if (j - coins[i] === 0) { | ||
toBeAdd = [[coins[i]]] | ||
} | ||
res[j] = [...res[j], ...toBeAdd] | ||
// res[j] = res[j-coins[i]].concat(res[j-coins[i]].map(it => [...it, coins[i]])) | ||
} | ||
} | ||
} | ||
|
||
return dp[amount]; | ||
} | ||
change(5, [1, 2, 5]) | ||
setTimeout(function () { | ||
console.log('9'); // 9 | ||
process.nextTick(function () { | ||
console.log('10'); // 11 | ||
}) | ||
new Promise(function (resolve) { | ||
console.log('11'); // 10 | ||
resolve(); | ||
}).then(function () { | ||
console.log('12') // 12 | ||
}) | ||
}) |
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,25 @@ | ||
// 给你一个长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 res ,其中 res[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积。 | ||
// 示例: | ||
// 输入: [1,2,3,4] | ||
// 输出: [24,12,8,6] | ||
|
||
// 提示:题目数据保证数组之中任意元素的全部前缀元素和后缀(甚至是整个数组)的乘积都在 32 位整数范围内。 | ||
// 说明: 请不要使用除法,且在 O(n) 时间复杂度内完成此题。 | ||
|
||
function f(nums) { | ||
let res = [] | ||
let tmp1 = Array(nums.length).fill(1) | ||
let tmp2 = Array(nums.length).fill(1) | ||
for (let i = 1; i <= nums.length - 1; i++) { | ||
tmp1[i] = tmp1[i - 1] * nums[i - 1] | ||
} | ||
res[nums.length - 1] = tmp1[nums.length - 1] | ||
for (let j = nums.length - 2; j >= 0; j--) { | ||
tmp2[j] = tmp2[j + 1] * nums[j + 1] | ||
res[j] = tmp1[j] * tmp2[j] | ||
} | ||
return res | ||
} | ||
console.log(f([1, 2, 3, 4])) | ||
console.log(f([0, 2, 6, 8])) | ||
console.log(f([-1, -2, 3, 4])) |