• 网址:https://leetcode.com/problems/unique-paths/

第一思路是动态规划

通过观察,每一个格子的路线数等于相邻的左方格子的路线数加上上方格子的路线数

于是我们就得到 dp[i][j] = dp[i-1][j] + dp[i][j-1] 这个状态转移方程

  • 一开始通过调用函数的方式对dp数组赋值,并且每次都判断index是否为特殊值,感觉耗时会很多,果不其然!
 int get_sum(vector<vector<int>> dp, int i, int j, int m, int n)
{
if(j == || i == )
return ;
return dp[i-][j] + dp[i][j-];
}
int uniquePaths(int m, int n)
{
vector<vector<int>> dp(m+,vector<int>(n+,));
dp[][] = -;
for(int i=; i<=m; i++)
{
for(int j=; j<=n; j++)
{
dp[i][j] = get_sum(dp, i, j, m, n);
}
}
return dp[m][n];
}

  • 紧接着想到可以在一开始就对特殊位置赋值,并且从零开始index,少去了函数的调用以及每一次的判断流程,程序可以大幅度减少运行时间和占用空间!
  •  class Solution {
    public:
    int uniquePaths(int m, int n) {
    vector<vector<int>> dp(m,vector<int>(n,));
    for(int i=;i<m;i++)
    dp[i][] = ;
    for(int j=;j<n;j++)
    dp[][j] = ;
    for(int i=; i<m; i++)
    {
    for(int j=; j<n; j++)
    {
    dp[i][j] = dp[i-][j] + dp[i][j-];
    }
    }
    return dp[m-][n-];
    }
    };

62. Unique Paths不同路径的更多相关文章

  1. LeetCode 62. Unique Paths不同路径 (C++/Java)

    题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

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

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

  4. 刷题62. Unique Paths

    一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...

  5. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

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

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

  8. LeetCode OJ 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 ...

  9. 【一天一道LeetCode】#62. Unique Paths

    一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...

随机推荐

  1. skype for business 无法共享桌面、无法传输图片

    以管理员身份运行如下PowerShell命令,清除Skype for Business缓存记录 #以管理员身份运行如下PowerShell命令,清除Skype for Business缓存记录 Sto ...

  2. Docker Builders:Builder pattern vs. Multi-stage builds in Docker

    原文链接 Builder pattern vs. Multi-stage builds in Docker This post looks at two new PRs from the Docker ...

  3. 主键非自增列 EF 插入数据库引起的 ID 列不能为 NULL 的错误

    protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<PostBo ...

  4. unity3d 加载优化建议 总结 from 侑虎科技

    第一部分 我们对于纹理资源的加载建议如下: 1.严格控制RGBA32和ARGB32纹理的使用,在保证视觉效果的前提下,尽可能采用“够用就好”的原则,降低纹理资源的分辨率,以及使用硬件支持的纹理格式. ...

  5. java 四种线程池的异同

    四种线程池的区别仅仅在于executors让threadpoolexecutor的构造器的参数不同,即核心线程池数,最大线程池数等不同.但是其他的,例如终止线程池等都是一样的

  6. [转]pugixml使用教程

    转自:https://www.cnblogs.com/ltm5180/p/3989125.html pugixml介绍 pugixml是一个高性能.轻量级并且简单易用的xml解析库,支持UTF8 en ...

  7. less 前端css利器 工程化、逻辑化 css 文件

    less LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承,运算, 函数. 1. 浏览器方式 1.1 html <!DOCTYPE html> <html lang=&q ...

  8. Linux环境下的Scala环境搭建

    1.下载tag软件包后,我习惯放到software文件夹下,并建立app文件夹2.通过tar -zxvf scala-2.12.8.tgz -C ~/app/ 命令解压到app目录下(-C 是指定目录 ...

  9. Eclipse Jee环境配置

    最近下载了新的Eclipse Jee Neon版本,记录一下如何进行开发环境的配置. 1.下载必要的开发环境文件 ①下载Java SE Development Kit (简称JDK) ②下载Tomca ...

  10. WmiPrvSe.exe 的 cpu 占用

    经常会看到这个进程cpu升上去,然后播放视频卡顿,鼠标移动卡顿. 1) 首先怀疑公司的Mcafee, 然后竟然检索除了一篇文章,MCafee表示不背锅. 2)找到这篇文章,微软表示,不能看表面,你得查 ...