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 ...
随机推荐
- 洛谷 P2048 [NOI2010]超级钢琴 || Fantasy
https://www.luogu.org/problemnew/show/P2048 http://www.lydsy.com/JudgeOnline/problem.php?id=2006 首先计 ...
- android:fillViewport="true"让ScrollView内的view强行match_parent
当你想让一个高度值不足scrollview的子控件fillparent的时候,单独的定义android:layout_height="fill_parent"是不起作用的,必须加上 ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree
二叉树的序列化与反序列化. 如果使用string作为媒介来存储,传递序列化结果的话,会给反序列话带来很多不方便. 这里学会了使用 sstream 中的 输入流'istringstream' 和 输出流 ...
- 1043 幸运号码 数位DP
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1043 设dp[i][j]表示前i位数中,i位数的和为j时的所有情况. 转 ...
- vuetifyjs简介及其使用
何为 vuetify 一个vue ui库,提供vue组件供使用.根据 Google Material Design 指南实现(https://material.io/).Vuetify支持SSR(服务 ...
- Angular广播/消息通知的接收与发送
一.在接收页:添加引用: private eventManager: JhiEventManager: 接收通知的方法: // 接收通知(新建.编辑.删除页发送过来的通知) // upmsMenuLi ...
- vscode显示php函数列表
1.安装插件支持 https://marketplace.visualstudio.com/items?itemName=linyang95.php-symbols 2.ctrt+shift+o 即可 ...
- System.Lazy<T>延迟加载
在很多情况下,有些对象需要在使用时加载或根据逻辑动态加载.有些情况如果不延迟加载,可能会影响效率甚至抛出Timeout Exception.如网络操作.数据库操作.文件IO操作 直接上代码,方便我们理 ...
- asterisk-java ami4 一些基本功能的例子
比如:挂机,拨号,拨外线,保留通话,示闲,示忙等等.... 在api中可以获得这些方法说明 /** * 给分机挂机 * @param Extension 要挂机的分机号 * @return * ...
- vba,设置,excel,wps ,页面设置例子
Sub yemian()'按钮1点击触发 执行下面的命令With Sheets(1).PageSetup'对像是表格1的页面设置的函数PageSetup,下面是函数的属性修改前面加. .Orienta ...