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 ...
随机推荐
- 使用Quartz实现定时作业
该文章是系列文章 基于.NetCore和ABP框架如何让Windows服务执行Quartz定时作业 的其中一篇. Quartz是一个开源的作业调度框架,准确的称谓应该是 Quartz.Net,它是Ja ...
- mysql远程备份
相关链接:https://blog.csdn.net/LiuHuan_study/article/details/81512831https://www.cnblogs.com/ryanzheng/p ...
- dalvik.system.VMRuntime 隐藏api的迷惑
[Android UI界面]关于dalvik.system.VMRuntime 的 使用迷惑 我也遇到了相同问题.不知楼主现在解决了没有? 回答1: [Android UI界面]关于dalvik.sy ...
- 16-2 基于localStorage或sessionStorage的计数器
localStorage 方法 localStorage 方法存储的数据没有时间限制.第二天.第二周或下一年之后,数据依然可用. <!doctype html> <html> ...
- Html标签杂记
<html> <head> <title> </title> </head> <body> </body> < ...
- Java操作pdf: JarsperReport的简单使用
在企业级应用开发中,报表生成.报表打印下载是其重要的一个环节.除了 Excel 报表之外,PDF 报表也有广泛的应用场景. 目前世面上比较流行的制作 PDF 报表的工具如下: iText PDF :i ...
- jQuery实现复选框的全选与全不选
对于复选框的选中checked属性,实在是无力吐槽. 从上图可以看出,当复选框不设置checked属性时,默认没有被选中:其它三种情况,设置checked属性但不设置属性值即置空,或者将checked ...
- 2019PAT春季考试第4题 7-4 Structure of a Binary Tree (30 分)
题外话:考试的时候花了一个小时做了27分,由于Siblings这个单词不知道意思,所以剩下的3分就没去纠结了,后来发现单词是兄弟的意思,气哭~~ 这道题的麻烦之处在于如何从一个字符串中去找数字.先首先 ...
- echarts 外观效果修改
<!DOCTYPE html> <html> <head> <title></title> <link rel="style ...
- CentOS 6.4 linux下编译安装MySQL5.6.14
CentOS 6.4下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.14. 正文: 一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm ...