leetcode----63. Unique Paths II

Follow up for “Unique Paths”:

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2.

Note: m and n will be at most 100.

版本一

public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length, n = obstacleGrid[0].length;
        int[][] dp = new int[m][n];
        for(int i = 0; i < m; i++){
            if(obstacleGrid[i][0] == 1) break;
            dp[i][0] = 1;
        }
        for(int i = 0; i < n; i++){
            if(obstacleGrid[0][i] == 1) break;
            dp[0][i] = 1;
        }
        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                if(obstacleGrid[i][j]==1){
                    dp[i][j]=0;
                }
                else{
                    dp[i][j]=dp[i-1][j] + dp[i][j-1];
                }
            }
        }
        return dp[m-1][n-1];
    }

版本2

public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        if(obstacleGrid == null || obstacleGrid.length==0 || obstacleGrid[0].length==0)  
            return 0; 
        int m = obstacleGrid.length, n = obstacleGrid[0].length;
        int [] dp=new int[n];
        dp[0]=1;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                if(obstacleGrid[i][j]==1){
                    dp[j]=0;
                }
                else if(j>0){
                    dp[j]+=dp[j-1];
                }
            }
        }
        return dp[n-1];
    }
你提供的代码是 LeetCode 中 `uniquePaths` 问题的一个标准 **动态规划解法**,用于计算一个 `m x n` 网格中从左上角到右下角的不同路径数量,每次只能向右或向下移动一步。 你提到使用 **Visual Studio 2022 (VS2022)**,这说明你可能在本地开发环境中运行这段代码时遇到了一些问题,比如编译错误、运行时错误、或者对某些语法不理解。 --- ### ✅ 正确代码(适用于 VS2022): 确保你包含了必要的头文件 `<vector>`,并且使用了 `std` 命名空间。 ```cpp #include <vector> using namespace std; class Solution { public: int uniquePaths(int m, int n) { vector<vector<int>> f(m, vector<int>(n)); for (int i = 0; i < m; ++i) { f[i][0] = 1; } for (int j = 0; j < n; ++j) { f[0][j] = 1; } for (int i = 1; i < m; ++i) { for (int j = 1; j < n; ++j) { f[i][j] = f[i - 1][j] + f[i][j - 1]; } } return f[m - 1][n - 1]; } }; ``` --- ### 🧪 VS2022 中运行建议: 1. **确保项目类型为 C++ 项目**(如控制台应用、空项目等)。 2. **在 main 函数中测试代码**: ```cpp #include <iostream> int main() { Solution sol; cout << sol.uniquePaths(3, 7) << endl; // 示例输入 return 0; } ``` 3. **编译和运行**: - 使用快捷键 `Ctrl + Shift + B` 编译项目。 - 使用 `Ctrl + F5` 或点击“本地 Windows 调试器”运行程序。 --- ### ❗常见问题及解决方法: #### ✅ 问题 1:报错 `vector` 未声明? - **原因**:未包含 `<vector>` 头文件或未使用 `std` 命名空间。 - **解决方法**:加上 `#include <vector>` 和 `using namespace std;`。 #### ✅ 问题 2:编译错误 C2059: 语法错误? - **原因**:可能将代码直接粘贴到了不支持 C++ 的 `.c` 文件中。 - **解决方法**:确保文件后缀为 `.cpp`,并设置为 C++ 编译模式。 #### ✅ 问题 3:运行时报错 `LNK2019: 无法解析的外部符号`? - **原因**:没有 `main` 函数或未正确设置项目类型。 - **解决方法**:确保有 `main()` 函数,且项目是可执行项目(控制台应用)。 --- ### ✅ 代码逻辑简要说明: - 使用二维数组 `f` 来记录每个位置 `(i, j)` 的路径数。 - 初始化第一行和第一列为 1,因为只有一种方式到达。 - 动态规划递推公式:`f[i][j] = f[i-1][j] + f[i][j-1]`。 - 最终结果是右下角的值 `f[m-1][n-1]`。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值