题意:

  给出一个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)的更多相关文章

  1. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

  2. [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 ...

  3. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  4. [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 ...

  5. Leetcode Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  6. 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 ...

  7. 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 ...

  8. [leetcode]Unique Paths @ Python

    原题地址:https://oj.leetcode.com/problems/unique-paths/ 题意: A robot is located at the top-left corner of ...

  9. [Leetcode] unique paths ii 独特路径

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  10. 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 ...

随机推荐

  1. 一段linux shell 代码涉及for循环和if esle

    if [ 0 -ne $# ]; then echo "USAGE: prog [IN]input_file" >&2; exit 1;fisource /etc/p ...

  2. Tomcat配置虚拟主机的两种方式

    1.基于主机名的虚拟主机配置 在随意盘符下建立一个目录作为虚拟地址的目录.例如:F:\virtualhost1,在其下建立 test1.html,写入内容例如:test 在tomcat/conf/se ...

  3. CI 目录下放置index.html,防止直接访问

    CI 目录下放置index.html,防止直接访问

  4. ASP.NET MVC的Ajax.ActionLink 的HttpMethod="Get" 一个重复请求的BUG

    这段时间使用BootStrap+Asp.net Mvc5开发项目,Ajax.ActionLink遇到一个重复提交的BUG,代码如下: @model IList<WFModel.WF_Temp&g ...

  5. android 计算器的实现(转)

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  6. 详解centos用户&组权限&添加删除用户

    1.Linux用户操作系统 Linux操作系统是多用户多任务操作系统,包括用户账户和组账户两种: 细分用户账户(普通用户账户,超级用户账户)除了用户账户以为还有组账户所谓组账户就是用户账户的集合,ce ...

  7. C++-sizeof和strlen的区别

    一.sizeof    sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型.对象.函数等.    它的功能是:获得保 ...

  8. 为什么要进行傅立叶变换?傅立叶变换究竟有何意义?如何用Matlab实现快速傅立叶变换

    写在最前面:本文是我阅读了多篇相关文章后对它们进行分析重组整合而得,绝大部分内容非我所原创.在此向多位原创作者致敬!!!一.傅立叶变换的由来关于傅立叶变换,无论是书本还是在网上可以很容易找到关于傅立叶 ...

  9. Java基础01 ------ 从HelloWorld到面向对象

    Java是完全面向对象的语言.Java通过虚拟机的运行机制,实现“跨平台”的理念.我在这里想要呈现一个适合初学者的教程,希望对大家有用. "Hello World!" 先来看一个H ...

  10. powershell命令大全

    Name Category Synopsis ---- -------- -------- ac Alias Add-Content asnp Alias Add-PSSnapin clc Alias ...