64. Minimum Path Sum

和unique path一样的

 1     public int minPathSum(int[][] grid) {
 2         if(grid == null || grid.length == 0 || grid[0].length == 0) {
 3             return 0;
 4         }
 5         int len = grid[0].length;
 6         int[] res = new int[len];
 7         res[0] = grid[0][0];
 8         for(int i = 1; i < len; i++) {
 9             res[i] = res[i-1]  + grid[0][i];
10         }
11         for(int i = 1; i < grid.length; i++) {
12             for(int j = 0; j < len; j++) {
13                 if(j == 0) {
14                     res[j] += grid[i][j];
15                 } else {
16                     res[j] = Math.min(res[j], res[j-1]) + grid[i][j];
17                 }
18             }
19         }
20         return res[len-1];
21     }

 

posted @ 2016-03-10 08:40  warmland  阅读(137)  评论(0编辑  收藏  举报