Sharing my neat C++ solution


  • 0
    B

    Very neat, very beautiful!

    class Solution {
    public:
        int minPathSum(vector<vector<int>>& grid) {
            int m = grid.size(), n = (m>0)?grid[0].size():0;
            if(m==0 || n==0) return 0;
            for(int i=0;i<m;i++) {
                for(int j=0;j<n;j++) {
                    if(i==0&&j==0) continue;
                    grid[i][j] += min((i==0)?INT_MAX:grid[i-1][j], (j==0)?INT_MAX:grid[i][j-1]);
                }
            }
            return grid[m-1][n-1];
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.