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. ubuntu16.04安装 java JDK8

    安装openjdk1.更新软件包列表: sudo apt-get update 2.安装openjdk-8-jdk: sudo apt-get install openjdk-8-jdk 3.查看ja ...

  2. 使用apache benchmark(ab) 测试报错: apr_socket_recv: Connection timed out (110)

    使用ab( apache benchmark )测试的时候,使用如下命令: ab -n 15000 -c 200   http://localhost/abc/abc.php 执行操作一定条数,或连续 ...

  3. 素数筛选:HDU2710-Max Factor

    Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...

  4. Android开发——AsyncTask的使用以及源码解析

    .AsyncTask使用介绍  转载请标明出处:http://blog.csdn.net/seu_calvin/article/details/52172248 AsyncTask封装了Thread和 ...

  5. BZOJ 5390: [Lydsy1806月赛]糖果商店

    F[i][j]表示总重量为i,最上面那个盒子中糖果种类为j的方案数 每次新加一个盒子,或者在原来盒子中加入一个糖 F[i][0]为中间状态,优化转移(表示最上面那个盒子不能加糖果) #include& ...

  6. Python选修课第二届Turtle绘图大赛~~画猫猫

    (a)20161401167 夏思敏 20161401179 段梦格 (b)代码执行视频链接 点击查看:Python使用turtle库画猫猫 (c)程序源码 import turtle turtle. ...

  7. [转] vuex最简单、最直白、最全的入门文档

    前言 我们经常用element-ui做后台管理系统,经常会遇到父组件给子组件传递数据,下面一个简单的例子,点击按钮,把弹框显示变量数据通过子组件的props属性传递,子组件通过$emit事件监听把数据 ...

  8. ogre3D学习基础9 -- 光源程序实例

    这一章练习一下光源的使用,光源分为三种:点光源,聚光源,有向光.具体内容前面说过,这里就不解释了. 继续在上一章的程序的基础上实现. 1.创建摄像机(Camera) createCamera()函数是 ...

  9. Flash中国地图 开放源码

    Flash中国地图,以Object为数据源,便于实现基于中国地图的可视化项目. 特征: swc,便于导入到Flex项目中 数据源为Object,比XML更方便 数据驱动的地图块颜色和Hover颜色 可 ...

  10. Zookeeper在windows环境下安装

    1.已安装JDK并配置好了环境变量 2.下载Zookeeper,在清华大学镜像下载,选择合适版本  https://mirrors.tuna.tsinghua.edu.cn/apache/zookee ...