刷题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 ...
随机推荐
- 对C#继承、多态的理解
11月3日 阴天 前两天看某位大牛写的程序,对于C#多态有困惑,今天一大早来查阅了不少资料,自认为有了一个基本的认知,记录下来,一扫今天这阴霾的天气 ------------------------- ...
- Flask 教程 第十七章:Linux上的部署
本文翻译自The Flask Mega-Tutorial Part XVII: Deployment on Linux 这是Flask Mega-Tutorial系列的第十七部分,我将把Microbl ...
- Lenet 神经网络-实现篇(2)
Lenet 神经网络在 Mnist 数据集上的实现,主要分为三个部分:前向传播过程(mnist_lenet5_forward.py).反向传播过程(mnist_lenet5_backword.py). ...
- gbase安装教程
一.安装前的准备工作 1.对网卡进行配置 [root@gbase8a ~]#vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 HWADD ...
- 类扩展欧几里得 zquoj 26659
求该式子,因为只有里面mod 外面没mod: 所以先是把前面的等差数列求和,然后再减去模掉的部分: 这是类欧几里得模板题 #include<bits/stdc++.h> #define ...
- Abaqus 载荷分类(部分)
目录 1. 集中载荷 1.1 集中载荷施加方法 1.2 定义集中跟随力 1.3 指定文件定义集中节点力 2. 分布载荷 2.1 分布载荷分类 3. 热载荷 3.1 模拟热辐射 3.2 直接定义热流量 ...
- CSS学习(2)Id和Class选择器
id 选择器 id 选择器可以为标有特定 id 的 HTML 元素指定特定的样式. HTML元素以id属性来设置id选择器,CSS 中 id 选择器以 "#" 来定义. 以下的样式 ...
- C语言如何判断单个数字是否溢出:
如何判断一个输入或者输出转化的单个数字是否溢出: if( num>0x7fffffff || num<(signed int)0x80000000) 注意: int类型的最大正数:0x7f ...
- asp.net 大文件上传配置
<system.web> <httpRuntime requestValidationMode=" ></httpRuntime> <!--单位:K ...
- 「JSOI2015」染色问题
「JSOI2015」染色问题 传送门 虽然不是第一反应,不过还是想到了要容斥. 题意转化:需要求满足 \(N + M + C\) 个条件的方案数. 然后我们就枚举三个数 \(i, j, k\) ,表示 ...