标签:
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
Array Dynamic Programming
这题是一个很简单的动态规划题
#include<iostream> #include<vector> using namespace std; int minPathSum(vector<vector<int>>& grid) { int m=grid.size(); int n=grid[0].size(); int **ary=new int*[m]; for(int i=0;i<m;i++) ary[i]=new int[n]; ary[m-1][n-1]=grid[m-1][n-1]; for(int i=n-2;i>=0;i--) ary[m-1][i]=grid[m-1][i]+ary[m-1][i+1]; for(int i=m-2;i>=0;i--) ary[i][n-1]=grid[i][n-1]+ary[i+1][n-1]; for(int i=m-2;i>=0;i--) for(int j=n-2;j>=0;j--) { ary[i][j+1]<ary[i+1][j]?ary[i][j]=ary[i][j+1]:ary[i][j]=ary[i+1][j]; ary[i][j]=ary[i][j]+grid[i][j]; } int a=ary[0][0]; for(int i=0;i<m;i++) delete []ary[i]; delete []ary; return a; } int main() { }
leetcode_64题——Minimum Path Sum(动态规划)
标签:
原文地址:http://www.cnblogs.com/yanliang12138/p/4560145.html