【leetcode】Array—— Minimum Path Sum(64)

题目:

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,比较容易

代码:

    	//init first row
    	int sum=0;
    	for(int i=0;i
后来想一下,不需要extra space,可以直接在grid数组里面操作

leetcode给的思路也是大同小异,虽然看起来简练了,但是运行速度降低了

    public int minPathSum(int[][] grid) {
    	int row=grid.length;int col=grid[0].length;
    	for(int i=0;i


你可能感兴趣的:(leetcode-array,leetcode,dynamic,programming)