题面

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. 在Excel多个工作表间快速切换的绝招

    在Excel多个工作表间快速切换的绝招 几乎每个Excel用户"数据分析师"都应该知道,如果一个Excel工作簿中包括许多个工作表,我们"数据分析师"可以通过单 ...

  2. iframe重定向让父页面跳转

    情景描述 我们在使用一些后台程序的html模板(比如H-ui)的时候,这些html前端程序是iframe版的, 也就是说在使用的时候,每当我点击左侧导航栏的一个按钮,在右侧就会弹出一个菜单栏,在显示的 ...

  3. html页面设置自动刷新

    在中添加如下: <meta HTTP-EQUIV="REFRESH" CONTENT="1"> 其中CONTENT对应刷新的间隔时间,这里为1秒. ...

  4. 欧姆龙_NX1P_PLC功能模块_限定安全作业空间

    程序  和说明 以及流程图 已上传到百度云盘 https://pan.baidu.com/s/1kYF11pQHJ4VxyaXr8E_gAA 主要是用来向量的叉乘  判断俩向量正向夹角

  5. (IStool)64位软件安装在32位操作系统时给出提示

    需求:64位的软件当在32位操作系统下安装时,需要提示用户不能在32位操作系统中进行安装 实现:打包时启用64位模式(打包工具用的是Inno Setup 5) 安装脚本段需要添加以下代码: [Setu ...

  6. MySQL的注入总结

    0x01 MySQL 5.0以上和MySQL 5.0以下版本的区别 MySQL5.0以上版本存在一个叫information_schema的数据库,它存储着数据库的所有信息,其中保存着关于MySQL服 ...

  7. 【ARM-Linux开发】Wi-Fi 应用工具wpa_supplicant

    wpa_supplicant是一个跨平台的无线安全管理软件,这里需要用它来对无线网络进行配置,wpa_supplicant相关工具已经移植好,包含在我们提供的文件系统中. 配置无线网络 wpa_sup ...

  8. MySQL 全局锁和表锁

    根据加锁的范围,MySQL 里面的锁大致可以分成全局锁,表级锁,行锁. 行锁已经在前面几篇文章说过 1. 全局锁 全局锁就是对整个数据库实例加锁.MySQL 提供了一个加全局读锁的方法,命令是Flus ...

  9. ES6 中 let 和 const 总结

    目录 let const 1. let要好好用 1. 基本用法 2. let声明的变量不存在变量提升 3. TDZ(temporal dead zone)暂时性死区 4. 不允许重复声明 2. 块级作 ...

  10. 最新 游族网络java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.游族网络等10家互联网公司的校招Offer,因为某些自身原因最终选择了游族网络.6.7月主要是做系统复习.项目复盘.Leet ...