题目

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?

Note: m and n will be at most 100.

分析

这是一道动态规划的题目:

对于一个m∗n矩阵,求解从初始点(0,0)到结束点(m−1,n−1)的路径条数,规定每次只能向右或向下走一步;

我们知道:

1.当i=0,j=[0,n−1]时,f(i,j)=f(i,j−1)=1; 因为每次只能向右走一步,只有一条路径;

2. 当i=[0,m−1],j=0时,f(i,j)=f(i−1,j)=1;因为每次只能向下走一步,只有一条路径;

3. 当(i,j)为其它时,f(i,j)=f(i−1,j)+f(i,j−1);因为此时可由(i−1,j)向右走一步,或者(i,j−1)向下走一步,为两者之和;

AC代码

//非递归实现回溯,会超时
class Solution {
public:
int uniquePaths(int m, int n) {
if (m == 0 || n == 0)
return 0;
vector<vector<int> > ret(m, vector<int>(n, 1));
//如果矩阵为单行或者单列,则只有一条路径
for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++)
ret[i][j] = ret[i - 1][j] + ret[i][j - 1]; return ret[m-1][n-1];
}
};

递归实现算法(TLE)

class Solution {
public:
int uniquePaths(int m, int n) {
if (m == 0 || n == 0)
return 0;
//如果矩阵为单行或者单列,则只有一条路径
else if (m == 1 || n == 1)
return 1; else
return uniquePaths(m, n - 1) + uniquePaths(m - 1, n);
}
};

GitHub测试程序源码

LeetCode(62)Unique Paths的更多相关文章

  1. LeetCode(63)Unique Paths II

    题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ...

  2. 【leetcode】62.63 Unique Paths

    62. Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the di ...

  3. LeetCode(62):不同路径

    Medium! 题目描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“F ...

  4. LeetCode(96) Unique Binary Search Trees

    题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ...

  5. LeetCode(95) Unique Binary Search Trees II

    题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ...

  6. LeetCode(96)Unique Binary Search Trees

    题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ...

  7. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  8. Qt 学习之路 2(62):保存 XML

    Home / Qt 学习之路 2 / Qt 学习之路 2(62):保存 XML Qt 学习之路 2(62):保存 XML  豆子  2013年8月26日  Qt 学习之路 2  9条评论 前面几章我们 ...

  9. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

随机推荐

  1. (拓展欧几里得)51NOD 1256 乘法逆元

    给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K < N且K * M % N = 1,如果有多个满足条件的,输出最小的.   输入 输入2个数M, N中间用空 ...

  2. 采购发票检验MIRO差异科目设置

    采购订单发票检验时,最终的金额可能跟采购订单的价格不一样,对于这部分差异,系统提供了后台配置科目的方式. 配置科目可通过OBYC,在BSX存货差异配置相关评估类型对应科目. 当库存商品少于采购订单数量 ...

  3. POJ 2833 The Average(优先队列)

    原题目网址:http://poj.org/problem?id=2833 本题中文翻译: 描述 在演讲比赛中,当选手完成演讲时,评委将对他的演出进行评分. 工作人员删除最高成绩和最低成绩,并计算其余成 ...

  4. 【react native】rn踩坑实践——从输入框“们”开始

    因为团队技术栈变更为react native,所以开始写起了rn的代码,虽然rn与react份数同源,但是由于有很多native有关的交互和变动,实际使用还是碰到蛮多问题的,于是便有了这个系列,本来第 ...

  5. Dexposed:android免Root无侵入Aop框架

    在网上看到了阿里推出的一个android开源项目,名为Dexposed, 是一个Android平台下的无侵入运行期AOP框架.旨在解决像性能监控.在线热补丁等移动开发常见难题,典型使用场景为: AOP ...

  6. BZOJ 3224 SBT 普通平衡树

    复习了一下SBT的模板,但是BZOJ不知道为什么注册不了,所以就没交,测了样例能过! #include <bits/stdc++.h> #include<algorithm> ...

  7. Neither BindingResult nor plain target object for bean name 'user' available as request attribute

    这个异常是因为jsp页面写错了. 把<form:form></form:form>标签改成普通的标签即可. 应该是第一次访问的时候,user是空的.但springmvc不能是空 ...

  8. JDK集合框架--ArrayList

    ArrayList,从类名就可以看出来,这是由数组实现的List,即内部是用数组保存元素的有序集合.先看看主要的成员变量,比较简单: public class ArrayList<E> e ...

  9. IO流的原理和概念

    在程序中如何读写文件?不同的编程语言有不同的方式,而 JAVA 则提出了“流”的概念,通过“流”来读写文件 什么是流: 流(Stream)是指一连串的数据(字符或字节),是以先进先出的方式发送信息的通 ...

  10. (2)《Head First HTML与CSS》学习笔记---img与基于标准的HTML5

    1.浏览器处理图像的过程: 1.服务器获取文件,显示出文本结构,以及预留默认的大小给<img>(如果该<img>有width-1值和height-1值,则根据这个值提前设好页面 ...