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 ...
随机推荐
- Python教程:[43]Word基本操作
使用python操作Word用到了win32com模块,我们现在就要介绍一下python对Word的基本操作,文章涉及到如何与Word程序建立连接.如果与Word文档建立连接的,以及对Word文档的基 ...
- HTML5关于上传API的一些使用(上)
HTML5提供了很多有用的API,其中就包括上传的API,XMLHttpRequest2.0,在HTML5时代之前,需要进行二进制的上传一般都会才用flash的方案,但是当XMLHttpRequest ...
- PHP libevent扩展安装
libevent是一个基于事件驱动的高性能网络库.支持多种 I/O 多路复用技术, epoll. poll. dev/poll. select 和 kqueue 等:支持 I/O,定时器和信号等事件: ...
- java----序列化与反序列化中及java序列化本质就是存储一个对象,然后在其他地方在调用它
Java 序列化Serializable详解(附详细例子) 1.什么是序列化和反序列化Serialization(序列化)是一种将对象以一连串的字节描述的过程:反序列化deserialization是 ...
- Linux Shell Vim 经常使用命令、使用技巧总结
前言 本文总结了自己实际开发中的经常使用命令,不定时更新,方便自己和其它人查阅. 如有其它提高效率的使用技巧.欢迎留言. 本文地址 http://blog.csdn.net/never_cxb/art ...
- epplus excel数据导出(数据量有点大的情况) Web和Client
Asp.net MVC后台代码 public ActionResult Export() { OfficeOpenXml.ExcelPackage ep = new OfficeOpenXml.Exc ...
- ie中自动识别单屏与双屏(js)
<!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...
- “几何画板+MathType”双11组合特价,优惠多多
工欲善其事,必先利其器!几何画板和MathType作为数学老师必备工具,在数学教学中起着非常重要的作用.为回馈老师们做出的伟大贡献,在双11狂欢节期间,MathType和几何画板迎来史上第一次组合特惠 ...
- 学习《深入理解C#》—— 委托的构成、合并与删除和总结 (第二章1.1---1.4)
目录 简单委托的构成 合并和删除委托 委托总结 简单委托的构成 委托四部曲: 声明委托类型. 必须有一个方法包含了要执行的方法. 必须创建一个委托实例. 必须调用委托(invoke)实例 ① 声明委托 ...
- Angular ViewChild
viewchild // 使用方法 git clone https://git.oschina.net/mumu-osc/learn-component.git cd learn-component ...