LeetCode Unique Paths
2015-09-13 07:47
399 查看
原题链接在这里:https://leetcode.com/problems/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 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
若从DP角度考虑这个问题就是需保存历史数据为走到当前格子的不同路径数,用二维数组res保存。
更新当前点res[i][j]为上一行同列res[i-1][j]的值 + 本行上一列res[i][j-1]的值,因为走到上一行同列的值想走到当前格都是往下走一步,左边同理。
初始条件是第一行和第一列都是1.
Time Complexity: O(m*n). Space: O(m*n).
AC Java:
存储历史信息可以用一维数组完成从而节省空间。生成一个长度为n的数组res, 每次更新res[j] += res[j-1], res[j-1]就是同行前一列的历史结果,res[j]为更新前是同列上一行的结果,所以res[j] += res[j-1]就是更新后的结果。
Note:外层loop i 从0开始,dp[0] = 1, 相当于初始了第一列,所以i=0开始要初始第一行
Time Complexity: O(m*n). Space: O(n).
有进阶版题目Unique Paths II.
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 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
若从DP角度考虑这个问题就是需保存历史数据为走到当前格子的不同路径数,用二维数组res保存。
更新当前点res[i][j]为上一行同列res[i-1][j]的值 + 本行上一列res[i][j-1]的值,因为走到上一行同列的值想走到当前格都是往下走一步,左边同理。
初始条件是第一行和第一列都是1.
Time Complexity: O(m*n). Space: O(m*n).
AC Java:
public class Solution { public int uniquePaths(int m, int n) { if(m == 0 || n == 0){ return 0; } int [][] dp = new int[m] ; for(int i = 0; i<m; i++){ dp[i][0] = 1; } for(int j = 0; j<n; j++){ dp[0][j] = 1; } for(int i = 1; i<m; i++){ for(int j = 1; j<n; j++){ dp[i][j] = dp[i-1][j] + dp[i][j-1]; } } return dp[m-1][n-1]; } }
存储历史信息可以用一维数组完成从而节省空间。生成一个长度为n的数组res, 每次更新res[j] += res[j-1], res[j-1]就是同行前一列的历史结果,res[j]为更新前是同列上一行的结果,所以res[j] += res[j-1]就是更新后的结果。
Note:外层loop i 从0开始,dp[0] = 1, 相当于初始了第一列,所以i=0开始要初始第一行
Time Complexity: O(m*n). Space: O(n).
public class Solution { public int uniquePaths(int m, int n) { if(m == 0 || n == 0){ return 0; } int [] dp = new int ; dp[0] = 1; for(int i = 0; i<m; i++){ for(int j = 1; j<n; j++){ dp[j] += dp[j-1]; } } return dp[n-1]; } }
有进阶版题目Unique Paths II.
相关文章推荐
- ImportError: Twisted requires zope.interface 3.6.0 or later: no module named zope.interface.
- [PHP学习教程 - 类库]001.全局唯一ID(GUID)
- ZOJ 3820 Building Fire Stations(二分+BFS)
- UIButton重复点击的bug解决方法
- 高仿阿里云后台系统模板,梦赢系统系统通用,EasyUI也能使用
- UI基础----退出键盘
- blueStacks模拟器竖屏调整
- Deep learning:四十四(Pylearn2中的Quick-start例子)
- Mediator模式-管理对象间交互映射/双向关联由Colleague子类驱动
- poj 1904 King's Quest 【建模 求解SCC】
- 用ueditor上传图片、文件等到七牛云存储
- 解决Error:No suitable device found: no device found for connection "Syst
- IT设备管理系统
- jquer学习之事件
- codeforces 264B B. Good Sequences(dp+数论)
- uva 1504 - Genghis Khan the Conqueror(生成树)
- Volley定制自己的Request
- uva 1464 - Traffic Real Time Query System(双联通+LCA)
- opengl 3.3做底层 QML做UI渲染
- UGUI 音效