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 ...
随机推荐
- .net core 问题:413 Request Entity Too Large nginx
https://stackoverflow.com/questions/38698350/increase-upload-file-size-in-asp-net-core The other ans ...
- eclipse创建springBoot项目
创建Spring Boot 工程 先在eclipse中安装spring -tool -suite插件,然后根据以下步骤可以创建1.新建Spring Starter Project 2.Packagin ...
- [学习一个] Matlab GUI 学习笔记 Ⅰ
Matlab GUI 学习笔记 Ⅰ 1. Foreword Matlab 是严格意义上的编程语言吗?曾经有人告诉我他是通过 Matlab 学会了面对对象编程,我是不信的,但这依然不妨碍它在特殊领域的强 ...
- windows下远程连接Mysql
使用“Ctrl + R”组合键快速打开cmd窗口,并输入“cmd”命令,打开cmd窗口. 使用“mysql -uroot -proot”命令可以连接到本地的mysql服务. 使用“use mysql” ...
- JAVA怎样理解面向对象
一.对象 现实世界中,随处可见的一种事物就是对象,对象是事物存在的实体,如人类.书桌.计算机.高楼大厦等.人类解决问题的方式总是将复杂的事物简单化,于是就会思考这些对象都是由哪些部分组成的.通常都 ...
- React native中的组建通知通信:
有这么一个需求,在B页面pop()回到A页面,需要A页面执行刷新,那么我们可以采用以下方法: 1:在A页面Push到B页面中,加上一个A页面中的刷新函数做为参数,然后在B页面中在pop()函数封装后通 ...
- Codeforces 781C Underground Lab
题目链接:http://codeforces.com/problemset/problem/781/C 因为有${K}$个机器人,每个又可以走${\left \lceil \frac{2n}{k} \ ...
- 在使用Java8并行流时的问题分析
最近在使用Java8的并行流时遇到了坑,线上排查问题时花了较多时间,分享出来与大家一起学习与自查 // 此处为坑 List<Java8Demo> copy = Lists.newArray ...
- Java——String,StringBuffer,StringBuilder
String 一经创建,不可更改,每次更改都是创建新对象,销毁旧对象 StringBuilder 创建后可修改,多线程不安全 StringBuffer 创建后可修改,多线程安全 StringBuffe ...
- PostgreSQL 扩展开发基础教程
搭建基础结构 安装扩展 sudo apt-get install postgresql-contribcreatedb stupsql stucreate extension pg_buffercac ...