刷题62. Unique Paths
一、题目说明
题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径。其中每次只能向下、向右移动。难度是Medium!
二、我的解答
这个题目读读题目,理解后不难。定义一个dp[m][n],初始化最后一列为1,最后一行为1,然后循环计算到dp[0][0]就可以了。
代码如下:
class Solution{
public:
int uniquePaths(int m,int n){
vector<vector<int>> dp(m,vector<int>(n,0));
//最后一行初始化为1
for(int i=0;i<n;i++){
dp[m-1][i] = 1;
}
//最后一列初始化为1
for(int i=0;i<m;i++){
dp[i][n-1] = 1;
}
for(int t=n-2;t>=0;t--){
for(int k=m-2;k>=0;k--){
dp[k][t] = dp[k+1][t]+dp[k][t+1];
}
}
return dp[0][0];
}
};
性能如下:
Runtime: 4 ms, faster than 56.78% of C++ online submissions for Unique Paths.
Memory Usage: 8.8 MB, less than 32.81% of C++ online submissions for Unique Paths.
三、优化措施
其他没有看到更好的解答,递归,回溯,都比较复杂。
刷题62. Unique Paths的更多相关文章
- [leetcode] 62 Unique Paths (Medium)
原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 62. Unique Paths(中等,我自己解出的第一道 DP 题^^)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 62. Unique Paths
题目: A robot is located at the top-left corner of a m x ngrid (marked 'Start' in the diagram below). ...
- 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 ...
- [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 ...
- [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 ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
随机推荐
- ASP.NET/C# Razor视图引擎深入浅出
在ASPX中我们使用 <% %>在里面编写C#代码在Razor中我们将会使用@{}编写C#代码1.基础——@+属性显示计算1+2的值:<span>1+2=@(1+2)</ ...
- 08day 操作命令以及目录结构
yum /var/log目录(日志文件)两个重要目录:message--记录系统或服务程序运行状态信息 secure--记录用户登录信息 tail -f 查看日志方法 head 查问文件头部
- 题解【SP1043】 GSS1 - Can you answer these queries I
题目描述 You are given a sequence \(A_1, A_2, ..., A_n(|A_i|≤15007,1≤N≤50000)\). A query is defined as f ...
- 每天进步一点点------FPGA 静态时序分析模型——reg2reg
2. 应用背景 静态时序分析简称STA,它是一种穷尽的分析方法,它按照同步电路设计的要求,根据电路网表的拓扑结构,计算并检查电路中每一个DFF(触发器)的建立和保持时间以及其他基于路径的时延要求是否满 ...
- IQueryable、IEnumberable 、IList与List区别
IEnumerable:使用的是LINQ to Object方式,它会将AsEnumerable()时对应的所有记录都先加载到内存,然后在此基础上再执行后来的Query IQeurable(IQuer ...
- Codeforces Round #577 (Div. 2) 题解
比赛链接:https://codeforc.es/contest/1201 A. Important Exam 题意:有\(n\)个人,每个人给出\(m\)个答案,每个答案都有一个分值\(a_i\), ...
- 《爬虫学习》(三)(requests库使用)
requests库 虽然Python的标准库中 urllib模块已经包含了平常我们使用的大多数功能,但是它的 API 使用起来让人感觉不太好,而 Requests宣传是 “HTTP for Human ...
- idea项目更改git地址
第一步:idea打开项目,菜单栏找VCS - Git - Remotes 点进去,弹出对话框,选中,点击编辑 弹出编辑框,更改地址,点击ok 弹出输入账号密码编辑框,输入自己的账号密码,点击确认 完成 ...
- 【代码学习】PYTHON装饰器
一.装饰器 对原代码不修改的基础上完善代码 写代码要遵循开放封闭原则,虽然在这个原则是用的面向对象开发,但是也适用于函数式编程,简单来说,它规定已经实现的功能代码不允许被修改,但可以被扩展,即: 封闭 ...
- 理解Spring 容器、BeanFactory 以及 ApplicationContext
一.spring 容器理解 spring 容器可以理解为生产对象(Object)的地方,在这里容器不只是帮助我们创建对象那么简单,它负责了对象的整个生命周期-创建.装配.销毁.而这里对象的创建管理的控 ...