题目:

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的更多相关文章

  1. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  2. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. [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 ...

  4. 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  ...

  5. [leetcode]Unique Paths II @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths-ii/ 题意: Follow up for "Unique Paths": N ...

  6. [leetcode]Unique Paths @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...

  7. [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 ...

  8. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  9. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  10. 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 ...

随机推荐

  1. 记一次线上MySQL数据库死锁问题

            最近线上项目报了一个MySQL死锁(DealLock)错误,虽说对业务上是没有什么影响的,由于自己对数据库锁这块了解不是很多,之前也没怎么的在线上碰到过.这次刚好遇到了,便在此记录一下 ...

  2. cocos2dx中的一些坑

    1.CCTableView中的lua绑定LUA_TableViewDataSource 在TestLua里有例子,有个TableView的例子function TableViewTestLayer.c ...

  3. Linux安装MediaWiki

    1.    编译安装libxml2 # wget http://xmlsoft.org/sources/libxml2-2.6.32.tar.gz # tar zxvf libxml2-2.6.32. ...

  4. [转]解决Cannot change version of project facet Dynamic web module to 2.5

    我们用Eclipse创建Maven结构的web项目的时候选择了Artifact Id为maven-artchetype-webapp,由于这个catalog比较老,用的servlet还是2.3的,而一 ...

  5. KMP + 求最小循环节 --- POJ 2406 Power Strings

    Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少 ...

  6. C#实例.net_经典例子400个

    一共470多例winform 界面特效的源码. 窗体与界面设计... 9 实例001  带历史信息的菜单    10 实例002  菜单动态合并    12 实例003  像开始菜单一样漂亮的菜单.. ...

  7. JavaScript有关的10个怪癖和秘密(转)

    数据类型和定义 -------------------------------------------------------------------------------------------- ...

  8. MathType怎么编辑双箭头

    很多的数学相关工作者在写文章或论文的时候常常会用到数学公式编辑器.MathType就是一款深受大家欢迎的公式编辑器.很多的用户在使用过程中会用到双箭头符号来表示推理过程,但是怎么编辑又不知道,下面本教 ...

  9. react-native新导航组件react-navigation详解

    http://blog.csdn.net/sinat_17775997/article/details/70176688

  10. python3.4 百度API接口

    # -*- coding: utf-8 -*- import urllib.request, json url = 'http://apis.baidu.com/netpopo/illegaladdr ...