题面

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. java判断字符串是否中文、日文

    直接上代码: import java.io.UnsupportedEncodingException; public class StringUtil { /** * 去掉字符串中的标点符号.空格和数 ...

  2. 算法习题---3.01猜数字游戏提示(UVa340)

    一.题目 实现一个经典“猜数字”游戏.给定答案序列和用户猜的序列,统计有多少数字位置正确(A),有多少数字在两个序列都出现过但位置不对(B). 输入包含多组数据.每组输入第一行为序列长度n,第二行是答 ...

  3. 使用Lock对象实现同步效果

    Lock是一个接口,为了使用一个Lock对象,需要用到   Lock lock = new ReentrantLock();   与 synchronized (someObject) 类似的,loc ...

  4. navigationBarTitleText

    想修改整个程序的导航栏,在app.json 文件 修改 "window": { "backgroundTextStyle": "light" ...

  5. iOS定时器按钮短时间内多次点击只触发一次事件方法

    今天在看别人代码的时候,有个个60秒获取验证码的功能,做了个定时器,按钮触发定时器,点击按钮后设置按钮的enabled为NO,逻辑来讲都是没问题的 但是实际操作的时候,恶意的在短时间内多次点击那个获取 ...

  6. C#登录SSH执行命令,下载文件

    前言 批量登录SSH执行命令 ,把应急响应中的日志文件下载回来. 代码实现 Renci.SshNet编译出DLL,引用. using System; using System.Collections. ...

  7. vim 中与编码有关的选项

    在 Vim 中,有四个与编码有关的选项,它们是:fileencodings.fileencoding.encoding 和 termencoding.在实际使用中,任何一个选项出现错误,都会导致出现乱 ...

  8. charles 偏好设置

    本文参考:charles 偏好设置 charles 偏好设置  偏好设置,注意作用如下 用户界面 视图 启动设置 警告设置 视图选项 头和主体一起查看 请求和响应页查看 结构试图布局 序列试图布局 显 ...

  9. vue的依赖注入provide和inject

    一.解决的场景问题: 根父组件A有一个方法getMap,该组件A下的所有子组件B,子组件C,子组件D,或者子组件B下的子组件E等层层嵌套情况下,在某种情况下,都需要访问父组件的getMap方法,那么常 ...

  10. 查询父节点parentNode

    示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <!DOCTYPE htm ...