[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 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?
Above is a 7 x 3 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
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
题目
给定MxN棋盘,只允许从左上往右下走,每次走一格。共有多少种走法?
思路
Matrix DP(二维DP) 问题
1. 初始化
预处理第一个row: dp[0][j] = 1 因为从左上起点出发,往右走的每一个unique path都是1
预处理第一个col: dp[i][0]= 1 因为从左上起点出发,往下走的每一个unique path 都是1
2. 转移方程
因为要求所有possible unique paths之和
dp[i][j] 要么来自dp[i-1][j] 要么来自dp[i][j-1]
代码
class Solution {
public int uniquePaths(int m, int n) {
int[][]dp = new int[n][m]; // [row][col] // 预处理第一个col
for(int i= 0; i < n; i++){
dp[i][0] = 1;
}
//预处理第一个row
for(int j=0; i < m; i++){
dp[0][j] = 1;
}
for(int i= 1; i < n; i++){
for(int j= 1; j < m; j++){
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}
}
return dp[n-1][m-1];
}
}
[leetcode]62. Unique Paths 不同路径的更多相关文章
- [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 ...
- 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). ...
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- [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 ...
- 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 ...
- [leetcode] 62 Unique Paths (Medium)
原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...
- LeetCode: 62. Unique Paths(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...
- 62. Unique Paths不同路径
网址:https://leetcode.com/problems/unique-paths/ 第一思路是动态规划 通过观察,每一个格子的路线数等于相邻的左方格子的路线数加上上方格子的路线数 于是我们就 ...
- 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). ...
随机推荐
- Entity Framework教程翻译 ---- 系列教程
Entity Framework教程(第二版) (翻译)Entity Framework技巧系列之十四 - Tip 56 (翻译)Entity Framework技巧系列之十三 - Tip 51 - ...
- 第二章 JavaScript文档(上)
JavaScript 1.JavaScript简介 起源 在1995年时,由Netscape公司的Brendan Eich,在网景导航者浏览器上首次设计实现而成.Netscape在最初将其脚本语言命名 ...
- win10 hyper-v 外网设置
1. hyper-v管理器中添加 [虚拟交换机] 2. 为虚拟机添加网络适配器 在[虚拟交换机]中选择新创建的交换机 3. 在本本的[网络连接]中,按下 CTRL 键,然后选择[WLAN]和[vEt ...
- Spark性能优化指南——高级篇
本文转载自:https://tech.meituan.com/spark-tuning-pro.html 美团技术点评团队) Spark性能优化指南——高级篇 李雪蕤 ·2016-05-12 14:4 ...
- AIUI开放平台:多轮对话返回前几轮语槽数据
编写云函数: AIUI.create("v2", function(aiui, err){ // 获取 response response = aiui.getResponse() ...
- xshell 显示中文
xshell 可能无法正常显示中文,即使选择了utf-8编码也不生效. 1:输入:echo $LANG 显示:en_US.UTF-8 2.输入:export LANG=zh_CN.UTF-8 3. 输 ...
- Unity外包团队:Daydream控制器只提供了3个自由度
HTC Vive,Oculus Rift以及微软即将推出的MR头显都拥有6自由度的运动控制器,这意味着你在虚拟世界中可以任意摆动你的手.然而,Daydream控制器只提供了3个自由度,这对于手部运动具 ...
- LVM快照备份与恢复
简介 最近在做数据备份方面的工作,用到两个工具,一个是dd,而另一个是lvm的快照(snapshot).由于数据比较大,直接是用dd非常耗时,而lvm的快照可以在几秒内轻松搞定,而且可以达到实时的 ...
- MySQL内存使用查看方式
使用版本:MySQL 5.7 官方文档 在performance_schema有如下表记录内存使用情况 mysql> show tables like '%memory%summary%'; + ...
- <Numerical Analysis>(by Timothy Sauer) Notes
2ed, by Timothy Sauer DEFINITION 1.3A solution is correct within p decimal places if the error is l ...