62. Unique Paths不同路径
- 网址:https://leetcode.com/problems/unique-paths/
第一思路是动态规划
通过观察,每一个格子的路线数等于相邻的左方格子的路线数加上上方格子的路线数
于是我们就得到 dp[i][j] = dp[i-1][j] + dp[i][j-1] 这个状态转移方程
- 一开始通过调用函数的方式对dp数组赋值,并且每次都判断index是否为特殊值,感觉耗时会很多,果不其然!
int get_sum(vector<vector<int>> dp, int i, int j, int m, int n)
{
if(j == || i == )
return ;
return dp[i-][j] + dp[i][j-];
}
int uniquePaths(int m, int n)
{
vector<vector<int>> dp(m+,vector<int>(n+,));
dp[][] = -;
for(int i=; i<=m; i++)
{
for(int j=; j<=n; j++)
{
dp[i][j] = get_sum(dp, i, j, m, n);
}
}
return dp[m][n];
}

- 紧接着想到可以在一开始就对特殊位置赋值,并且从零开始index,少去了函数的调用以及每一次的判断流程,程序可以大幅度减少运行时间和占用空间!
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> dp(m,vector<int>(n,));
for(int i=;i<m;i++)
dp[i][] = ;
for(int j=;j<n;j++)
dp[][j] = ;
for(int i=; i<m; i++)
{
for(int j=; j<n; j++)
{
dp[i][j] = dp[i-][j] + dp[i][j-];
}
}
return dp[m-][n-];
}
};
62. Unique Paths不同路径的更多相关文章
- LeetCode 62. Unique Paths不同路径 (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- [LeetCode] 62. Unique Paths 唯一路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [leetcode]62. Unique Paths 不同路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 刷题62. Unique Paths
一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- [LeetCode] 62. Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- LeetCode 62. Unique Paths(所有不同的路径)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- LeetCode OJ 62. Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 【一天一道LeetCode】#62. Unique Paths
一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...
随机推荐
- yyyy-MM-dd'T'HH:mm:ss.SSS'Z'即UTC时间,与String日期转换
本文为博主原创,未经允许不得转载: 最近在使用一个时间插件的时候,接收到的时间格式是 ’2017-11-27T03:16:03.944Z’ ,当我进行双向数据绑定的时候,由后台传过来的时间绑定到时间 ...
- matplotlib python
#导入包 import matplotlib.pyplot as plt import numpy as np # 从[-1,1]中等距去50个数作为x的取值 x = np.linspace(-1, ...
- 解析Django路由层URLconf
目录: 一 Django中路由的作用 二 路由的分组 三 路由分发 四 反向解析 五 名称空间 六 Django2.0版的path 一.Django中路由的作用 URL配置(URLconf ...
- Model中时间格式化
MVC 中 @Html中的时间格式化 @Html.TextBoxFor(model => model.StartTime, "{0:yyyy-MM-dd HH:mm:ss}" ...
- echo -n 和echo -e 参数意义
echo -n 不换行输出 $echo -n "123" $echo "456" 1 2 最终输出 123456 而不是 123 456 1 2 3 4 5 6 ...
- 用 Anaconda 完美解决 Python2 和 python3 共存问题
Python3 被越来越多的开发者所接受,同时让人尴尬的是很多遗留的老系统依旧运行在 Python2 的环境中,因此有时你不得不同时在两个版本中进行开发,调试. 如何在系统中同时共存 Python2 ...
- 项目Alpha冲刺--4/10
项目Alpha冲刺--4/10 1.团队信息 团队名称:基于云的胜利冲锋队 成员信息 队员学号 队员姓名 个人博客地址 备注 221500201 孙文慈 https://www.cnblogs.com ...
- 关于python的“重载”
首先,关于python和java的区别: 1.Java有是通过方法名和方法列表来定义一个函数,python是通过方法名来定义一个函数(不允许方法名相同的函数存在) 2.java是通过定义多个相同方法名 ...
- centos7 (ifconfig不能使用) -bash: ifconfig: command not found
[root@localhost ~]# ifconfig -bash: ifconfig: command not found 输入ip addr 确认IP地址是否设置正常,设置好如下所示,如果没有获 ...
- Java+selenium 爬Boss直聘中职位信息,薪资水平和职位描述
需要下载合适的selenium webdirver jar包和对应浏览器的驱动jar包 import org.openqa.selenium.By; import org.openqa.selen ...