码迷,mamicode.com
首页 > 编程语言 > 详细

Java for LeetCode 064 Minimum Path Sum

时间:2015-05-16 11:45:30      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

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.

解题思路:

dp问题,和上一题一样,JAVA实现如下:

	static public int minPathSum(int[][] grid) {
		int[] v = new int[grid[0].length];
		v[0]=grid[0][0];
		for (int i = 1; i < v.length; i++)
			v[i] = grid[0][i]+v[i-1];
		for (int i = 1; i < grid.length; i++) {
			v[0] += grid[i][0];
			for (int j = 1; j < v.length; j++)
				v[j] = Math.min(v[j], v[j - 1]) + grid[i][j];
		}
		return v[v.length - 1];
	}

 

Java for LeetCode 064 Minimum Path Sum

标签:

原文地址:http://www.cnblogs.com/tonyluis/p/4507458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!