Skip to content

Commit

Permalink
feat: 增加 63. 不同路径 II
Browse files Browse the repository at this point in the history
  • Loading branch information
fxss5201 committed Aug 23, 2024
1 parent d854ca8 commit 6feb1fe
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/.vitepress/leetcodeItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ const leetcodeItems = [
"text": "62. 不同路径",
"link": "/leetcode/uniquePaths"
},
{
"text": "63. 不同路径 II",
"link": "/leetcode/uniquePathsIi"
},
{
"text": "83. 删除排序链表中的重复元素",
"link": "/leetcode/removeDuplicatesFromSortedList"
Expand Down
1 change: 1 addition & 0 deletions docs/leetcode/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- [59. 螺旋矩阵 II](./spiralMatrixIi)
- [61. 旋转链表](./rotateList)
- [62. 不同路径](./uniquePaths)
- [63. 不同路径 II](./uniquePathsIi)
- [83. 删除排序链表中的重复元素](./removeDuplicatesFromSortedList)
- [2085. 统计出现过一次的公共字符串](./countCommonWordsWithOneOccurrence)
- [2182. 构造限制重复的字符串](./constructStringWithRepeatLimit)
Expand Down
12 changes: 12 additions & 0 deletions docs/leetcode/uniquePathsIi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 63. 不同路径 II

[63. 不同路径 II](https://leetcode.cn/problems/unique-paths-ii/description/)

## 代码

::: code-group

<<< ../../src/leetcode/uniquePathsIi/javascript.js{javascript} [javascript]
<<< ../../src/leetcode/uniquePathsIi/typescript.ts{typescript} [typescript]

:::
23 changes: 23 additions & 0 deletions src/leetcode/uniquePathsIi/javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 63. 不同路径 II:https://leetcode.cn/problems/unique-paths-ii/description/
// 输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
// 输出:2

export function uniquePathsIi (obstacleGrid) {
if (!obstacleGrid.length) {
return 0
}
const m = obstacleGrid.length
const n = obstacleGrid[0].length
const f = new Array(n).fill(0)
f[0] = 1
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (obstacleGrid[i][j]) {
f[j] = 0
} else {
f[j] = j === 0 ? f[j] : f[j] + f[j - 1]
}
}
}
return f[n - 1]
}
23 changes: 23 additions & 0 deletions src/leetcode/uniquePathsIi/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 63. 不同路径 II:https://leetcode.cn/problems/unique-paths-ii/description/
// 输入:obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
// 输出:2

export function uniquePathsIi (obstacleGrid: number[][]): number {
if (!obstacleGrid.length) {
return 0
}
const m = obstacleGrid.length
const n = obstacleGrid[0].length
const f: number[] = new Array(n).fill(0)
f[0] = 1
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (obstacleGrid[i][j]) {
f[j] = 0
} else {
f[j] = j === 0 ? f[j] : f[j] + f[j - 1]
}
}
}
return f[n - 1]
}
13 changes: 13 additions & 0 deletions test/leetcode/uniquePathsIi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect, test } from 'vitest'
import { uniquePathsIi } from '../../src/leetcode/uniquePathsIi/typescript.ts'
import { uniquePathsIi as uniquePathsIiJs } from '../../src/leetcode/uniquePathsIi/javascript.js'

test(`uniquePathsIi`, () => {
expect(uniquePathsIi([])).toBe(0)
expect(uniquePathsIi([[0,0,0],[0,1,0],[0,0,0]])).toBe(2)
})

test(`uniquePathsIiJs`, () => {
expect(uniquePathsIiJs([])).toBe(0)
expect(uniquePathsIiJs([[0,0,0],[0,1,0],[0,0,0]])).toBe(2)
})

0 comments on commit 6feb1fe

Please sign in to comment.