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 ...
随机推荐
- BZOJ3308 九月的咖啡店
Orz PoPoQQQ 话说这题还有要注意的地方... 就是...不能加SLF优化,千万不能加 n = 40000,不加本机跑出来2sec,加了跑出来40sec...[给跪了 /*********** ...
- [整][转]Invoke和BeginInvoke的使用
在Invoke或者BeginInvoke的使用中无一例外地使用了委托Delegate. 一.为什么Control类提供了Invoke和BeginInvoke机制? 关于这个问题的最主要的原因已经是do ...
- nginx日志切割
脚本完成后将其存入 Nginx 安装目录的 sbin 中,取名为 cut-log.sh,之后使用 crontab -e 新增一个定时任务,在其中增加执行这个脚本: #!/bin/bash ## 零点执 ...
- 【STL】-priority_queue的用法
初始化: priority_queue<int> maxPQ; priority_queue<int,vector<int& ...
- 数据结构-bubble sort
#gcc version 4.5.3 (GCC) #include <iostream> #include <algorithm> template <typename ...
- Android打开新的Activity并同时关闭当前Activity
Intent it = new Intent(); it.setClass(EditActivity.this, MainActivity.class); it.setFlags(Intent.FLA ...
- 8种主要排序算法的C#实现
作者:胖鸟低飞 出处:http://www.cnblogs.com/fatbird/ 简介 排序算法是我们编程中遇到的最多的算法.目前主流的算法有8种. 平均时间复杂度从高到低依次是: 冒泡排序(o( ...
- 在T-SQL中访问远程数据库(openrowset、opendatasource、openquery)
1. 启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前要先启用Ad Hoc Distributed Queries服务,因为这个服 ...
- Tengine安装配置
为了加快网站的大流量访问速度,公司要求把Nginx更换为Tengine,下面记录下整个安装配置过程: #安装必要依赖 yum install -y pcre pcre-devel zlib zlib- ...
- 关于doctype
一:html文档类型 doctype为documentype 的简称,是在html页面中声明的XHTML或者HTML的文件类型,正确准确的文件类型的声明,才能使html标签以及CSS样式生效. 声明文 ...