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项目在Docker上运行,内存占用过多导致pods重启的问题
默认情况下,.NET Core应用的内存回收模式是Server模式,这种情况下,内存占用和服务器核心数量有关,一半占用量比较大. 我们的应用目前吞吐量都不大,可以采用Workstation模式,这种模 ...
- pyqt5 eric6 pyqt5-tools
他们都可以通过pip安装,pyqt5-tool提供了qtdesigner,
- 正则解析json数据
http://tool.chinaz.com/regex http://tool.oschina.net/regex/
- JVM介绍
1. 什么是JVM? JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来 ...
- hdu 5795 A Simple Nim 博弈sg函数
A Simple Nim Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Pro ...
- centos nginx 中安装ssl证书 以及在项目中的使用
今天阿里云的证书到期了,重新申请了一个,下面是从申请到安装以及结合项目使用的过程: 1.登录阿里云 2.在左侧找到SSL证书 3.申请免费的证书 4.下载证书 5.根据说明配置nginx 6.在项 ...
- Python open操作文件
操作文件 # coding=utf-8 支持中文 """ China Japan France England """ f = open(& ...
- django信号 signal
django自带一套信号机制来帮助我们在框架的不同位置之间传递信息.也就是说,当某一事件发生时,信号系统可以允许一个或多个发送者(senders)将通知或信号(signals)发送给一组接受者(rec ...
- lattice 作图 举例
library(lattice) Depth <- equal.count(quakes$depth, number=8, overlap=.1)xyplot(lat ~ long | Dept ...
- leecode第三十三题(搜索旋转排序数组)
class Solution { public: int search(vector<int>& nums, int target) { int len=nums.size(); ...