LeetCode(62)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? 
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);
    }
};LeetCode(62)Unique Paths的更多相关文章
- LeetCode(63)Unique Paths II
		题目 Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. Ho ... 
- 【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 ... 
- LeetCode(62):不同路径
		Medium! 题目描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“F ... 
- LeetCode(96) Unique Binary Search Trees
		题目 Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For exam ... 
- LeetCode(95) Unique Binary Search Trees II
		题目 Given n, generate all structurally unique BST's (binary search trees) that store values 1-n. For ... 
- LeetCode(96)Unique Binary Search Trees
		题目如下: Python代码: def numTrees(self, n): """ :type n: int :rtype: int """ ... 
- 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 ... 
- Qt 学习之路 2(62):保存 XML
		Home / Qt 学习之路 2 / Qt 学习之路 2(62):保存 XML Qt 学习之路 2(62):保存 XML 豆子 2013年8月26日 Qt 学习之路 2 9条评论 前面几章我们 ... 
- LeetCode(275)H-Index II
		题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ... 
随机推荐
- ssh 公钥登录远程主机 并禁止密码登录
			https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys-on-centos7 如果在新的机器上,得先用密码登录一 ... 
- 压力测试之jmeter使用
			我很早之前就会使用jmeter,一直以为压力测试很简单,知道真正去做才明白,真正的压力测试并不只是会用jmeter而已.我现在才明白:会工具并不等同于会压力测试.对于压力测试需要补充的知识还有很多.. ... 
- MySQL调优之数据类型选择原则
			本文涉及:高可用数据库设计时数据类型的选择原则 在进行数据库设计时,如果能够选择最恰当的数据类型就可以为后期的数据库调优打好最坚实的基础 选择数据类型的原则 更小的通常更好 例如存储订单状态字段很多时 ... 
- 从零开始通过idea插件将一个spring boot项目部署到docker容器里运行
			实操:将一个spring boot项目部署到docker容器里运行 实验需要的环境: 腾讯云+Ubuntu 16.04 x64+idea+插件docker integration+daocloud 第 ... 
- Poj 3436 ACM Computer Factory (最大流)
			题目链接: Poj 3436 ACM Computer Factory 题目描述: n个工厂,每个工厂能把电脑s态转化为d态,每个电脑有p个部件,问整个工厂系统在每个小时内最多能加工多少台电脑? 解题 ... 
- 题解报告:hdu1994利息计算
			题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1994 Problem Description 为自行解决学费,chx勤工俭学收入10000元以1年定期 ... 
- DEV—【GridControl添加按钮列】
			效果图 打开GridControl的Run Designer在左侧栏中找到: 添加一个ButtonEdit: 更改属性中的值:Caption为按钮上显示的Text:Kind为按钮的类型: 然后拖到最后 ... 
- 开发一个 Web App 必须了解的那些事
			在过去的一年里,我在从头开始开发我的第一个重要的Web应用.经验教会了很多以前不知道的东西,特别是在安全性和用户体验方面. 值得一提的是,我上一次尝试构建的任何合理复杂性是在2005年.所以,在安全防 ... 
- mysql之通过cmd连接远程数据库
			---恢复内容开始--- 目录 前提 连接远程数据库 前提: 本地安装了mysql数据库 本地和远程网络是连通的,通过命令ping ip (即ping 192.168.0.333),可以ping通 连 ... 
- CSS3 按钮特效(一)
			1. 实例 2.HTML 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset ... 
