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 ...
随机推荐
- 在sql中根据成绩显示学生排名
1.准备 create table newtable ( name ), yuwen ), shuxue ) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; , ); , ...
- redis使用场景介绍
一:缓存——热数据 热点数据(经常会被查询,但是不经常被修改或者删除的数据),首选是使用redis缓存,毕竟强大到冒泡的QPS和极强的稳定性不是所有类似工具都有的,而且相比于memcached还提供了 ...
- keepalive的作用
keepalive的作用是实现高可用,通过VIP虚拟IP的漂移实现高可用.在相同集群内发送组播包,master主通过VRRP协议发送组播包,告诉从主的状态. 一旦主挂了从就选举新的主,实现高可用 LV ...
- Istio微服务架构初试
感谢 http://blog.csdn.net/qq_34463875/article/details/77866072 看了一些文档,有些半懂不懂,所以还是需要helloworld一下.因为isti ...
- JS调用asp.net后台方法:PageMethods
先帮朋友宣传一下程序人生(http://www.manong123.com)的网站,里面都是开发感悟,开发人员创业,支持一下吧~ 原来是通过PageMethods来实现的. 举个列子: Default ...
- C语言 百炼成钢22
/* 题目58: 编写一个业务函数,实现按行读取文件.把内容按照第三种内存模型打包数据传出,把行数通过函数参数传出. 函数原型有两个,任意选择其一 要求1:请自己任意选择一个接口(函数),并实现功能: ...
- 【BZOJ】1692 & 1640: [Usaco2007 Dec]队列变换(后缀数组+贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1692 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- html-include
import header <head> <link rel="import" href="header.html"> </hea ...
- js获取url中指定参数的值(含带hash)
function getUrlVars() { var vars = {}; var parts = window.location.href.replace(/[?&]+([^=&] ...
- java.lang.ClassNotFoundException: org.codehaus.jackson.JsonProcessingException 异常解决方案
在SpringMVC中使用Jackson实现json输出时配置如下: <!-- 输出对象转JSON支持 --> <bean id="stringConverter" ...