File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ title : lint110-minimum-path-sum
3+ tags : 新建,模板,小书匠
4+ grammar_cjkRuby : true
5+ ---
6+
7+
8+ # problem
9+ [ lin110-minimum-path-sum] ( http://www.lintcode.com/en/problem/minimum-path-sum/ )
10+ # solution
11+
12+ ``` cpp
13+ int minPathSum (vector<vector<int > > &grid) {
14+ // write your code here
15+ if (grid.empty() || grid[ 0] .empty())
16+ return 0;
17+ int m = grid.size();
18+ int n = grid[ 0] .size();
19+ //下面开始进行遍历;
20+ vector<int > minSums = vector<int >(n, 0);
21+ //初始化第一行
22+ minSums[ 0] = grid[ 0] [ 0 ] ;
23+ for (int i = 1; i<n;i++)
24+ minSums[ i] = minSums[ i-1] + grid[ 0] [ i ] ;
25+ //接下来每一行都比较左边和上边哪个值比较大;
26+ for (int i =1; i < m; i++)
27+ for (int j = 0; j < n;j++)
28+ if (j > 0 && minSums[ j] > minSums[ j-1] )
29+ minSums[ j] = minSums[ j-1] + grid[ i] [ j ] ;
30+ else
31+ minSums[ j] += grid[ i] [ j ] ;
32+ return minSums[ n-1] ;
33+ }
34+
35+ ```
You can’t perform that action at this time.
0 commit comments