题面

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

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?

Note: m and n will be at most 100.

说白了就是:统计从二维数组左上角到右下角总共有多少不同路径。(0 <= m, n <= 100)

样例

Example 1:

Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right

Example 2:

Input: m = 7, n = 3
Output: 28

思路

由于只能朝下或者右走,稍加推导,我们就可以看出:当前点的路径数就等于它左边点路径数加上上边点路径数,很容易想到递归(很不幸,层数过大,栈会溢出!)。so, 我们只能通过DP循环来做。

算法 : DP

时间复杂度:O(m*n)

空间复杂度:O(m*n)

1. 用二维数组还是一维数组记录状态都可以,我们先用二维来说明问题。即:创建二维数组dp[m][n]

2. 预处理第一行和第一列,因为第一行只能往右走,第一列只能往下走(只有一条路径,所以都初始化为1)

3. 遍历二维DP数组:当前点路径=上边点路径+左边点路径

状态方程:

dp[0][j] = 1

dp[i][0] = 1

dp[i][j] = dp[i-1][j] + dp[i][j-1]

源码

 int uniquePaths(int m, int n) {
if(m == || n == )
return ;
int dp[n][m] = {};
dp[][] = ;
for(int i=; i<m; i++)
dp[][i] = ;
for(int i=; i<n; i++)
dp[i][] = ;
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
dp[i][j] = dp[i][j-] + dp[i-][j];
}
}
return dp[n-][m-];
}

优化:空间优化

上面算法,我们使用了二维数组记录DP状态,其实用一维就够了。推到一个简单的例子你就会发现,焦点总是在一行上,只要用一行从上到下滑动,就可达到目的。

时间复杂度:O(m*n)

空间复杂度:O(n)

源码

     int uniquePaths(int m, int n) {
//空间压缩
if(m == || n == )
return ;
int dp[m] = {};
for(int i=; i<m; i++)
dp[i] = ;
for(int i=; i<n; i++)
{
dp[] = ;
for(int j=; j<m; j++)
{
dp[j] = dp[j-] + dp[j];
}
}
return dp[m-];
  }

leetcode-62. Unique Paths · DP + vector的更多相关文章

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

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

  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. [leetcode] 62 Unique Paths (Medium)

    原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...

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

  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不同路径 (C++/Java)

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

  8. LeetCode: 62. Unique Paths(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...

  9. LeetCode 63. Unique Paths II不同路径 II (C++/Java)

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

随机推荐

  1. 关于采样率&位深&码率&无损的一些心得

    转载自:https://blog.csdn.net/Marenow/article/details/85253283 记笔记,记下来自己的一些关于音频基础知识的总结. 采样率外界的声音都是模拟信号,在 ...

  2. laravel中redis pipeline用法说明

    $res = Redis::pipeline(function($pipe) use($params) { for ($i = 0; $i < 1000; $i++) { $pipe->g ...

  3. SQLPrompt 6.3.0.354 完美破解 安装于 SQL Server 2012/2014

    SQL SERVER 2012格式化 SQL SERVER 2014格式化 SQLPrompt_6.4.0.641 破解版 百度云下载 迅雷下载 百度网盘下载 SQL Prompt 是一款拥有SQL智 ...

  4. Vue.js—60分钟快速入门

    本文摘自:http://www.cnblogs.com/keepfool/p/5619070.html Vue.js是当下很火的一个JavaScript MVVM库,它是以数据驱动和组件化的思想构建的 ...

  5. gcc/clang编译带pthread.h头文件的源码时需要的参数

    今天敲了一个小程序,编译时出现错误:undefined reference pthread_create 原来由于pthread库不是Linux系统默认的库,连接时需要使用库libpthread.a, ...

  6. 工作总结 CTO(张王岩) File构造器

    import java.io.File; /** * 构建File对象 * @author Allen17805272076 * */ public class FileDemo2 { public ...

  7. 解决无/var/log/messages 问题

    转载于:https://blog.csdn.net/C_Major/article/details/51321684 1 内核编程insmod后,Ubuntu查看日志无/var/log/message ...

  8. 路由 router-view

    路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的about 按钮,页面中就要显示about 的内容.Home按钮 => home 内容, ...

  9. protobuf 序列化 使用

    protocol buff是一种协议,是谷歌推出的一种序列化协议 .Java序列化协议也是一种协议 两者的目的是,将对象序列化成字节数组,或者说是二进制数据 导包 <dependency> ...

  10. python标准库之collections介绍

    collections----容器数据类型 collections模块包含了除list.dict.和tuple之外的容器数据类型,如counter.defaultdict.deque.namedtup ...