题目

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. js判断是否为ie浏览器,精确显示各个ie版本

    function IETester(userAgent){     var UA =  userAgent || navigator.userAgent;     if(/msie/i.test(UA ...

  2. EL表达式(详解)

    EL表达式 1.EL基本内容 1)语法结构        ${expression} 2)[]与.运算符      EL 提供.和[]两种运算符来存取数据.      当要存取的属性名称中包含一些特殊 ...

  3. 用Python解析HTML,BeautifulSoup使用简介

    Beautiful Soup,字面意思是美好的汤,是一个用于解析HTML文件的Python库.主页在http://www.crummy.com/software/BeautifulSoup/ , 下载 ...

  4. Python字符串对象常用方法

    安利一句话:字符串是不可变的对象,所以任何操作对原字符串是不改变的! 1.字符串的切割 def split(self, sep=None, maxsplit=-1): # real signature ...

  5. Git客户端使用教程

    课程地址 <版本控制入门 – 搬进 Github> 笔记参考 <搬进 Github> Git客户端的使用 Git for windows下载 新建一个仓库tata,使用subl ...

  6. sed练习简记

    1. 使用多命令选项-e sed -e 'command1' -e 'command2' -e 'command3' 在/etc/passwd文件中搜索root.nobody或mail [root@s ...

  7. MySQL+PHP配置 Windows系统IIS版

    MySQL+PHP配置 Windows系统IIS版 1.下载 MySQL下载地址:http://dev.mysql.com/downloads/mysql/5.1.html->Windows ( ...

  8. json三层解析(数组解析)

    里面多了数组,所以用到了JOSNArray package com.xykj.weather; import java.io.BufferedReader; import java.io.IOExce ...

  9. Collection接口框架图

    Java集合大致可分为Set.List和Map三种体系,其中Set代表无序.不可重复的集合:List代表有序.重复的集合:而Map则代表具有映射关系的集合.Java 5之后,增加了Queue体系集合, ...

  10. 程序员的职业方向: 是-->技术?还是-->管理?

    岁之后还能不能再做程序员....... 绝大多数程序员最终的职业目标可能都是CTO,但能做到CEO的人估计会比较少,也有一少部分人自己去创业去当老板,也有部分人转行了,当老板的人毕竟是少数,转行的人都 ...