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 ...
随机推荐
- PHP 循环一个文件下的所有目录以及文件
function test($dir) { //判断dir是否目录 if(is_dir($dir)) { $files = []; //列出 dir 目录中的文件和目录: $list = scandi ...
- 《windows核心编程系列》二十二谈谈修改导入段拦截API。
一个模块的导入段包含一组DLL.为了让模块能够运行,这些DLL是必须的.导入段还包含一个符号表.它列出了该模块从各DLL中导入的符号.当模块调用这些导入符号的时候,系统实际上会调用转换函数,获得导入函 ...
- Python实现判断回文串
回文数的概念:即是给定一个数,这个数顺读和逆读都是一样的.例如:121,1221,a,aa是回文数,123,1231不是回文数. while 1: String = input('请先输入一个字符串 ...
- SAMP论文学习
SAMP:稀疏度自适应匹配追踪 实际应用中信号通常是可压缩的而不一定为稀疏的,而且稀疏信号的稀疏度我们通常也会不了解的.论文中提到过高或者过低估计了信号的稀疏度,都会对信号的重构造成影响.如果过低估计 ...
- DFS(深度) hihoCoder挑战赛14 B 赛车
题目传送门 题意:中文题面 分析:放官方题解,就是从1为根节点深搜记录节点的深度,选出最大的深度的点,将该到达该点的节点都vis掉,然后再重新计算没有vis的点的深度,找最大的相加就是答案.放张图好理 ...
- sqlserver事务隔离
事务是一个工作单元,可能包含查询和修改数据以及修改数据定义等多个活动.我们可以显式或隐式的定义事务边界.可以使用BEGIN TRAN或者BEGIN TRANSACTION语句显式的定义事务的开始.如果 ...
- Service官方教程(4)两种Service的生命周期函数
Managing the Lifecycle of a Service The lifecycle of a service is much simpler than that of an activ ...
- E. The Values You Can Make 背包,同时DP
http://codeforces.com/problemset/problem/688/E 题目需要在n个数中找出一个集合,使得这个集合的和为val,然后问这些所有集合,能产生多少个不同的和值. 题 ...
- 后缀数组 (Suffix Array) 学习笔记
\(\\\) 定义 介绍一些写法和数组的含义,首先要知道 字典序 . \(len\):字符串长度 \(s\):字符串数组,我们的字符串存储在 \(s[0]...s[len-1]\) 中. \(suff ...
- Codeforces_789C_(dp)
C. Functions again time limit per test 1 second memory limit per test 256 megabytes input standard i ...