[Leetcode] unique paths 独特路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
How many possible unique paths are there?
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
题意:从左上角到右下角,总共有多少条路径。
思路:动态规划。维护一个二维数组,dp[i][j]代表到达第i 行j列有几种方法。转移方程为:dp[i][j]=dp[i-1][j]+dp[i][j-1]。如图:
代码如下:
class Solution {
public:
int uniquePaths(int m, int n)
{
if(m=||n=) return ;
int dp[m][n];
dp[][]=; for(int i=;i<m;++i)
dp[i][]=;
for(int i=;i<n;++i)
dp[][i]=; 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-];
}
};
同种思路:另一种方法,使用滚动数组,利用一个一维数组实现上述过程,改动的思路是,二维数组中的dp[i][j]的值是表中的前一个和上一个,可以看成一维数组中的,前一个和其本身。
class Solution {
public:
int uniquePaths(int m, int n)
{
vector<int> dp(n,);
for(int i=;i<m;++i)
{
for(int j=;j<n;++j)
{
dp[j]+=dp[j-];
}
}
return dp[n-];
}
};
[Leetcode] unique paths 独特路径的更多相关文章
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [Leetcode] unique paths ii 独特路径
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] 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 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] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
随机推荐
- koa2 mongdb 做后端接口的小demo
现在前端全栈里面有一种技术栈比较火 前端使用 vue 或者react 后端使用 koa2 mysql数据库 或者mongdb做数据储存 但是基本这样的全栈教程 都要收费 收费就收费吧 但是 有没有遇到 ...
- centos搭建SVN服务
Linux VM_47_236_centos 3.10.0-514.21.1.el7.x86_64 需求:android.ios.service三个版本库 安装: yum -y install sub ...
- python -pickle模块、re模块学习
pickel模块 import pickle #pickle可以将任何数据类型序列化,json只能列表字典字符串数字等简单的数据类型,复杂的不可以 #但是pickle只能在python中使用,json ...
- CentOS(Linux)安装KETTLE教程 并配置执行定时任务
1,首先是安装jdk,并设置环境变量 采用yum安装可不设置环境变量 2,下载kettle https://sourceforge.net/projects/pentaho/files/Data%20 ...
- Kubernetes-apiserver
Kubernetes API服务器为API对象验证和配置数据,这些对象包含Pod.Service.ReplicationController等等.API Server提供REST操作以及前端到集群的共 ...
- Prime Ring Problem (DFS练习题)
K - Prime Ring Problem ============================================================================= ...
- java使用urlConnection抓取部分数据乱码
使用urlconnection做抓取的同学应该一开始都是使用这个吧.OK回到正题来..... 在内容己有中文.英文己正常显示,仍然会有部分中文或英文出现乱码,这是为什么呢?这个问题一直在心里盘旋... ...
- Django中的select_related与prefetch_related
Django是一个基于Python的网站开发框架,一个很重要的特点就是Battery Included,简单来说就是包含了常规开发中所需要的一切东西,包括但不限于完整的ORM模型.中间件.会话处理 ...
- SPOJ SUBLEX
SUBLEX - Lexicographical Substring Search 链接 题意 求第k小的子串.相同的算一个. 分析 建立后缀自动机,在后缀自动机上从一个点经过trans,到另一个点, ...
- 用ServiceStack操作使用redis的问题
最近在学习Redis,查阅网上很多资料后使用SericeStack连接redis.在nuget中下载ServiceStack.Redis,主要使用到四个dll 但是运行之后会出现一堆奇怪问题:没有实现 ...