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. ubuntu : 无法安全地用该源进行更新,所以默认禁用该源。

    sudo apt update报错: 无法安全地用该源进行更新,所以默认禁用该源. 1.检查是否是网络出了问题,修改DNS:114.114.114.114,8.8.8.8 断开网卡再重新连接,成功! ...

  2. NodeJS基础入门-Event

    大多数Node.js核心API都采用惯用的异步事件驱动架构,其中某些类型的对象(触发器)会周期性地触发命名事件来调用函数对象(监听器). 例如,net.Server对象会在每次有新连接时触发事件;fs ...

  3. python 取余运算

    python中取余运算逻辑如下: 如果a 与d 是整数,d 非零,那么余数 r 满足这样的关系: a = qd + r , q 为整数,且0 ≤ |r| < |d|. 经过测试可发现,pytho ...

  4. 面向对象之元类(metaclass)

    一.前言: 要搞懂元类必须要搞清楚下面几件事: 类创建的时候,内部过程是什么样的,也就是我们定义类class 类名()的过程底层都干了些啥 类的调用即类的实例化过程的了解与分析 我们已经知道元类存在的 ...

  5. 【python学习】新手基础程序练习(一)

    首先得先编一下程序员必须编的程序——Hello World……(这应该是程序员情结...) #coding=utf-8 #Version:python3.7.0 #Tools:Pycharm 2017 ...

  6. LeetCode(123) Best Time to Buy and Sell Stock III

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  7. 水题:CF16C-Monitor

    Monitor 题目描述 Reca company makes monitors, the most popular of their models is AB999 with the screen ...

  8. Linux学习-开机过程的问题解决

    忘记 root 密码的解决之道 新版的 systemd 的管理机制中,默认的 rescue 模式是无法直接取得 root 权限的喔!还是得要 使用 root 的密码才能够登入 rescure 环境.没 ...

  9. Python 前端的第三方库

    sweetalert sweeralert:地址 这个使用很简单,需要在在他们的,css和js文件. 酷炫的结果    datatables datatables:地址 已https://datata ...

  10. ThreeJs 基础入门

    本文来自网易云社区 作者:唐钊 Three.js 是一款运行在浏览器中的 3D 引擎,你可以用它在 web 中创建各种三维场景,包括了摄影机.光影.材质等各种对象.使用它可以让我们更加直观的了解 we ...