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.
题解:题目使用动态规划思想,result[i][j]表示从(0,0)点开始到(i,j)点的不同路径数,易知result[0][0] = 0;从start(0,0)到第一行或第一列的每个位置都是直走,只有一条路(一直往右走或一直往下走),所以result[0][j]=1,1<=j<=n-1;result[j][0]=1,1<=j<=m-1;对于(i,j)点(1<=i<=m-1,1<=j<=n-1),到达这一点或者是从上方的点(i-1,j)而来,或者是从左边的点(i,j-1)而来,所以result[i][j] =
result[i-1][j]+result[i][j-1],有了递归关系式,代码如下:
class Solution {
public:
int uniquePaths(int m, int n) {
int result[m][n];
result[0][0] = 0;
if(m==1&&n==1)return 1;
for(int i = 1;i<=n-1;i++)
result[0][i] = 1;
for(int i = 1;i<=m-1;i++)
result[i][0] = 1;
for(int i = 1;i<m;i++)
for(int j = 1;j<n;j++)
{
result[i][j] = result[i-1][j]+result[i][j-1];
}
return result[m-1][n-1];
}
};
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 不同的路径
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 I & II & Minimum Path Sum
Title: https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m ...
- [leetcode]Unique Paths II @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...
- [leetcode]Unique Paths @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...
- [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
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- 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 ...
随机推荐
- RabbitMQ之路由(Routing)【译】
在上一节中,我们创建了一个简单的日志系统,可以广播消息到很多接收者. 这一节,我们将在上一节的基础上加一个功能--订阅部分消息.例如,我们只将严重错误信息写入到日志文件保存在磁盘上,同时我们能将所有的 ...
- C++ 类的深拷贝和浅拷贝完美解决
//类的深拷贝和浅拷贝 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class Poin ...
- Unity的 Stats 窗体, Batched、SetPass、Draw Call 等
孙广东 2015.8.12 在Game View 中的右上角有一个统计数据 Stats button.当按下button时.覆盖窗体显示,可用于优化性能的实时渲染统计信息. 确切的统计数据显示生成目 ...
- 查看SQLServer数据库每个表占用的空间大小
创建存储过程: CREATE PROCEDURE [dbo].[sys_viewTableSpace] AS BEGIN SET NOCOUNT ON; CREATE TABLE [dbo].#tab ...
- 【openwrt+arduion】案例
http://www.geek-workshop.com/thread-4950-1-1.html http://www.guokr.com/article/319356/ http://www.gu ...
- mysql查询各种类型的前N条记录
mysql查询各种类型的前N条记录,将3改为N(需查询条数)即可 (select * from event_info where event_type = 1 limit 3)union all( ...
- 剑指 offer set 16 数字在排序数组中出现的次数
总结 1. Leetcode 上有一道题, 求某一个数字在有序数组中出现的最左位置和最右位置, 而这道题就是那题的变形
- Spring中bean的作用范围
singleton作用域: Spring的scope的默认值是singleton Spring 只会为每一个bean创建一个实例,并保持bean的引用. <bean id="bean的 ...
- php做图片上传功能
今天来做一个图片上传功能的插件,首先做一个html文件:text.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition ...
- 【BZOJ1116】[POI2008]CLO 并查集
[BZOJ1116][POI2008]CLO Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. ...