LeetCode Unique Paths (简单DP)
题意:
给出一个m*n的矩阵,robot要从[1][1]走到[m][n],每次只能往下/右走,问有多少种走法?
思路:
DP的经典问题。先将[1][1]设为1,然后两种走法就是分别用[i][j]去更新[i+1][j]和[i][j+1]。
观察一下数组的更新次序就可以知道,这很像完全背包的更新方式,那么就可以用一维数组就行了。更新方式从左到右就行了。
由于这是DP题,就没有必要去研究数学公式了(涉及等差数列求和)。
class Solution {
public:
int uniquePaths(int m, int n) {
vector<int> p(n,);
for(int i=; i<m; i++)
for(int j=; j+<n; j++)
p[j+]+=p[j];
return p[n-];
}
};
AC代码
LeetCode Unique Paths (简单DP)的更多相关文章
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )
Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...
- [LeetCode] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode] 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 Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LeetCode: 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 | 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]Unique Paths @ Python
原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...
- [Leetcode] unique paths ii 独特路径
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- LeetCode:Unique Paths I II
Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...
随机推荐
- PHP中MySql函数收集
1.array mysql_fetch_assoc ( resource $result ) 从结果集中取得一行作为关联数组 说明: 返回对应结果集的关联数组,并且继续移动内部数据指针. 参数:re ...
- noip知识点总结之--线性筛法及其拓展
一.线性筛法 众所周知...线性筛就是在O(n)的时间里找出所有素数的方法 code: void get_prime(int N){ int i, j, k; memset(Flag, ); ; i ...
- eclipse debug时老提示edit source lookup path解决方案
用myeclipse debug web应用的时候,总提示edit source lookup path,每次都得手动选择项目,费时费力.在网上终于找到了方法. 搬运:http://www.educi ...
- 简单模拟QQ界面框架。
package com.lixu.qqjiemian; import java.util.Timer; import java.util.TimerTask; import android.app.A ...
- jQuery中的quickExpr
jQuery 源码中的 quickExpr 是用来检测参数 selector 是否是复杂的 HTML 代码(如“abc<div>”)或 #id,匹配结果存放在数组 match 中 // A ...
- 百度360争推1TB永久网盘
导读:百度云.360云盘的互掐则将云盘空间战拉升到史无前例的高度——无论是国内还是国外,还没有哪家公司有过这样的手笔. 这几天百度热热闹闹开大会,会场外“小伙伴们”也不甘寂寞. ...
- 一键制作u盘启动盘教程
第一步:制作完成u深度u盘启动盘 第二步:下载Ghost Win7系统镜像文件包,存入u盘启动盘 第三步:电脑模式更改成ahci模式,不然安装完成win7系统会出现蓝屏现象 正式安装步骤: u ...
- svn自动更新
果对svn不熟悉,当svn上面有更新时,想看到实时效果,就得去web目录手动更新,比较麻烦 其它svn有一个自动更新的功能 利用 hook 在svn 仓库目录下面有一个hook目录 在post-c ...
- Object Oriented Programming python
Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...
- ASIHTTPRequest 在release(打包)模式下数据获取或post失败问题
ASIHTTPRequest 在relase模式下失效 表现为,调用网络请求后没有任何反应 原因之一: ARC模式下,在ASIHTTPRequest 前面会加上__weak来解决循环应用,这个__we ...