64.最小路径和

链接:

64.最小路径和

思路:

dp[i][j]=min(dp[i-1][j],dp[i][j-1])+grid[i][j];

实现:

class Solution {
public:
    int minPathSum(vector> &grid) {
        int m = grid.size();
        vector row = grid.at(0);
        int n = row.size();
        int path[200][200];
        memset(path, 0, sizeof(path));
        int cnt = 0;
        path[0][0] = grid[0][0];
        for (int i = 1; i < m; i++) {
            path[i][0] = path[i - 1][0] + grid[i][0];
           
        }

        for (int j = 1; j < n; j++) {
            path[0][j] = path[0][j - 1] + grid[0][j];

        }

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                path[i][j] = min(path[i - 1][j], path[i][j-1]) + grid[i][j];

            }

        }
        return path[m - 1][n - 1];

    }

    int min(int x, int y) {
        return x < y ? x : y;
    }
};

你可能感兴趣的:(64.最小路径和)