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 ...
随机推荐
- notepad++无法设置成默认打开方式
安装软件自动保存到默认的目录下(c盘下)
- 【牛客-14602】xinjun与阴阳师(01背包)
xinjun与阴阳师 题目描述 xinjun是各类手游的狂热粉丝,因随手一氪.一氪上千而威震工大,现在他迷上了阴阳师.xinjun玩手游有一个习惯,就是经过层层计算制定出一套方案来使操作利益最大化(因 ...
- 查找MySQL和 SQL sever data
MySql SQL server
- [UOJ311]积劳成疾
题解 dp 似乎这个最大值不好设计状态啊== 但是可以发现这\(n\)个点每个点都是相同的 可以设计状态\(f_{i,j}\)表示一个长度为\(i\)的一段区间的最大值不会超过\(j\)的价值 那么转 ...
- 校赛F 比比谁更快(线段树)
http://acm.cug.edu.cn/JudgeOnline/problem.php?cid=1153&pid=5 题意:给你一个字符串,各两个操作: ch=0,[l,r]降序 ch=1 ...
- ACM_求N^N的最高位数
Leftmost Digit Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定一个正整数N,你应该输出N ^ N的最左边的数字 ...
- 题解报告:hdu 1576 A/B(exgcd、乘法逆元+整数快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n ...
- Service官方教程(1)Started与Bound的区别、要实现的函数、声明service
Services 简介和分类 A Service is an application component that can perform long-running operations in the ...
- webapp开发学习---Cordova环境搭建
Cordova 使用HTML, CSS & JS进行移动App开发;多平台共用一套代码;免费开源 步骤:(来自Cordova官网) 1.安装Cordova(在node.js环境下进行安装) 命 ...
- ASP.NET URLRewriter重写
URLRewriter重写是微软官方出的第三方重写插件 下载地址:http://download.csdn.net/detail/ysn1314/5421587 下载后在项目中添加引用,然后再配置文件 ...