https://oj.leetcode.com/problems/minimum-path-sum/

对一个grid从左上角到右下角的路径,求出路径中和最小的。

受之前思路的影响,就寻思递归,并且记录中间过程的数据,这样避免重复计算。但是超时了。

class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
vector<vector<int> > sum;
if(grid.size()==)
return ;
int row = grid.size();
int col = grid[].size();
sum.resize(row);
for(int i = ;i<row;i++)
sum[i].resize(col);
return calcPath(,,grid,row,col,sum);
}
int calcPath(int xPos,int yPos,vector<vector<int> > &grid,int row,int col, vector<vector<int> > &sum)
{
if(xPos == row- && yPos == col-)
return grid[xPos][yPos];
int min1 = -,min2 = -;
if(xPos < row-)
if (sum[xPos+][yPos] == )
min1 = calcPath(xPos+,yPos,grid,row,col,sum);
else
min1 = sum[xPos+][yPos];
if(yPos < col-)
if(sum[xPos][yPos+] == )
min2 = calcPath(xPos,yPos+,grid,row,col,sum);
else
min2 = sum[xPos][yPos+];
if(min1 == -)
return min2;
if(min2 == -)
return min1;
return min1<min2?min1:min2;
}
};

其实,这个递归也是动态规划的思想。

但是,动态规划也可以用for循环做,于是清理思路,动态规划,for循环实现。

class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
vector<vector<int> > sum;
if(grid.size()==)
return ;
int row = grid.size();
int col = grid[].size();
sum.resize(row);
for(int i = ;i<row;i++)
sum[i].resize(col); //initialize
sum[row-][col-] = grid[row-][col-];
for(int i = col-;i>=;i--)
sum[row-][i] += grid[row-][i] + sum[row-][i+];
for(int i = row-;i>=;i--)
sum[i][col-] += grid[i][col-] + sum[i+][col-]; for(int i = row-; i>=;i--)
for(int j = col-;j>=;j--)
{
int t1 = grid[i][j] + sum[i][j+];
int t2 = grid[i][j] + sum[i+][j];
sum[i][j] = t1<t2?t1:t2;
}
return sum[][];
} };

LeetCode OJ--Minimum Path Sum **的更多相关文章

  1. [Leetcode Week9]Minimum Path Sum

    Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...

  2. 【leetcode】Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  3. 【LeetCode OJ】Path Sum II

    Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...

  4. 【LeetCode OJ】Path Sum

    Problem Link: http://oj.leetcode.com/problems/path-sum/ One solution is to BFS the tree from the roo ...

  5. LeetCode 64. Minimum Path Sum(最小和的路径)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  6. leetcode 【 Minimum Path Sum 】python 实现

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  7. [LeetCode] 64. Minimum Path Sum 最小路径和

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  8. LeetCode 64 Minimum Path Sum

    Problem: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...

  9. LeetCode OJ 112. Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  10. 【leetcode】Minimum Path Sum(easy)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

随机推荐

  1. cin 和 getline 混用中需要注意的问题

    这段时间在刷题过程中遇到一个cin和getline混合使用中的问题,解决之后记录如下: 先来看一段代码 #include <iostream> #include <string> ...

  2. 创建 Django 步骤

    1.创建项目 django-admin startproject 项目名称 2.创建APP python manage.py startapp app名称 3.修改settings.py文件 3.1设 ...

  3. phpstudy2016+phpstorm2017-3+xdebug+chrome

    1. 勾选Xdebug 后 phpstudy 会自动重启服务 [XDebug] xdebug.profiler_output_dir="D:\phpStudy\tmp\xdebug" ...

  4. my购物车

    sum=0 a=input("请输入“水果”或“衣服”:") if a=="手机": while True: shop = { '蓝葡萄', '水蜜桃', '草 ...

  5. python之路-双下方法

    双下方法 定义: 双下方法是特殊方法,他是解释器提供的,由双下线加方法名加双下划线 __方法名__具有特殊意义的方法 双下方法主要是Python源码程序员使用的,元编程 我们在开发中尽量不要使用双下方 ...

  6. LeetCode(307) Range Sum Query - Mutable

    题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...

  7. GPIO程序在PC上的模拟学习

    #include <stdio.h> #include <malloc.h> #include <memory.h> typedef struct gpio { i ...

  8. Power Calculus UVA - 1374 迭代加深搜索

    迭代加深搜索经典题目,好久不做迭代加深搜索题目,拿来复习了,我们直接对当前深度进行搜索,注意剪枝,还有数组要适当开大,因为2^maxd可能很大 题目:题目链接 AC代码: #include <i ...

  9. socketserver源码剖析

    作者:人世间链接:https://www.jianshu.com/p/357e436936bf來源:简书简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处 BaseServer 和 B ...

  10. java事物

    [引用:http://www.cnblogs.com/kristain/articles/2038397.html] 一.什么是事务 事务是访问数据库的一个操作序列,数据库应用系统通过事务集来完成对数 ...