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 ...
随机推荐
- 这不是bug,而是语言特性
分析编程语言缺陷的一种方法是把所有的缺陷归于3类:不该做的做了,该做的没做,该做但做得不合适. 在使用switch case时,如果使用缺省的 fall through,请一定在旁边注释,因为97%的 ...
- Spring整合redis,通过sentinel进行主从切换
实现功能描述: redis服务器进行Master-slaver-slaver-....主从配置,通过2台sentinel进行failOver故障转移,自动切换,采用该代码完全可以直接用于实际生产环境. ...
- 用c++封装linux系统调用
#include <pthread.h> #include <cstdlib> #include <ctime> #include <iostream> ...
- ansible之并行运行
ansible之并发运行 转载 2016年07月14日 12:33:39 标签: 并发 / ansible 4474 ansible默认只会创建5个进程,所以一次任务只能同时控制5台机器执行.那如果你 ...
- html 处理
近期做了一个后台管理网站,后台页面都是Html页面,里面再通过ajax访问后台服务.要做到比较好的用户体验,即:如果用户没有登录或没有权限马上调到登录页面,而不是等到页面加载后再ajax时判断是否登录 ...
- 让rm命令提示确认后再删除
首先在~/.bashrc文件中添加一行: # User specific aliases and functionsalias rm='rm -i' 注意,此处 rm 和 = 之间不能有空格,否则会有 ...
- zabbix服务器搭建
zabbix服务器源码安装 参看官方文档 这里不做过多的翻译,我的系统是centos6.5,安装的时候是base安装,所以要装一些其他依赖包,除此之外,还有一些php插件: extension=bcm ...
- linux_shell_find命令
使用find查找文件 基本格式:find path expression 1.按照文件名查找 (1)find / -name httpd.conf #在根目录下查找文件httpd.conf,表示在整个 ...
- 转:Socket服务器整体架构概述
Socket服务器主要用于提供高效.稳定的数据处理.消息转发等服务,它直接决定了前台应用程序的性能.我们先从整体上认识一下Socket服务器,Socket服务器从架构上一般分为:网络层.业务逻辑层.会 ...
- ChemDraw中化学信息怎么通过Excel搜索
用户可以通过ChemDraw for Excel插件功能在Office Excel中建立ChemOffice菜单将ChemOffice和Excel结合使用,使用电子表格的最大优势之一就是可以清晰查看并 ...