mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-22 13:00:56 -04:00
1.3 KiB
1.3 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 5900f37b1000cf542c50fe8e | 问题 15:网格路径 | 1 | 301780 | problem-15-lattice-paths |
--description--
从一个 2 × 2 的网格左上角出发,规定只允许向右或向下移动,则到达右下角的路径恰好有 6 条。
给定网格大小为 gridSize,请求出这样的路径有多少条?
--hints--
latticePaths(4) 应该返回一个数字。
assert(typeof latticePaths(4) === 'number');
latticePaths(4) 应该返回 70。
assert.strictEqual(latticePaths(4), 70);
latticePaths(9) 应该返回 48620。
assert.strictEqual(latticePaths(9), 48620);
latticePaths(20) 应该返回 137846528820。
assert.strictEqual(latticePaths(20), 137846528820);
--seed--
--seed-contents--
function latticePaths(gridSize) {
return true;
}
latticePaths(4);
--solutions--
function latticePaths(gridSize) {
let paths = 1;
for (let i = 0; i < gridSize; i++) {
paths *= (2 * gridSize) - i;
paths /= i + 1;
}
return paths;
}