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. 有重复元素的排列(DFS)

    题目描述: 设R={ r1, r2 , …, rn}是要进行排列的n个元素.其中元素r1, r2 , …, rn均为小写字母并且可能相同.试设计一个算法,列出R的所有不同排列. 给定n 以及待排列的n ...

  2. 素材网站——mokuge

  3. linux :没有找到 ifconfig netstat

    linux :没有找到 ifconfig netstat ubuntu sudo apt install net-tools -y centos yum install net-tools

  4. NFS搭建

    一.环境 nfsserver01:192.168.127.100  centos7.3 nfsclient01:192.168.127.101  centos7.3 二.NFS原理 三.安装测试 1. ...

  5. Linux菜鸟起飞之路【八】文本编辑器

    在Linux中,文本编辑器有两个,VI和VIM.这两个编辑器用法差不多,但vim是vi的升级版,所以功能更强大一些. vim编辑器一共有三种模式,命令行模式.编辑模式和扩展模式. 进入vim界面,首先 ...

  6. python入门:输出1-10的所有数

    #!/usr/bin/env python # -*- coding:utf-8 -*- #输出1-10的所有数 """ 变量kaishi的赋值为数字1,while 真, ...

  7. PHP数组函数 array_multisort() ----对多个数组或多维数组进行排序

    PHP中array_multisort可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序. 关联(string)键名保持不变,但数字键名会被重新索引. 输入数组被当成一个表的列并以 ...

  8. 【Linux命令】nohup和&差异,查看进程和终止进程!

    最近在开发dueros的技能,官方提供的PHPSDK中有多个实力,而运行实例的命令如下是 nohup php -S 0.0.0.0:8029 myindex.php & 从命令来看,肯定是在8 ...

  9. Python 中的for,if-else和while语句

    for 循环 功能 for 循环是一种迭代循环机制,迭代即重复相同的逻辑操作,每次的操作都是基于上一次的结果而进行的.并且for循环可以遍历任何序列的项目,如一个列表或者一个字符串 语法 for 循环 ...

  10. bootmem_init_node

    static unsigned long __init bootmem_init_node(int node, struct meminfo *mi) in arch/arm/mm/init.c 1. ...