LeetCode Unique Paths
原题链接在这里: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问题.需保存历史数据为走到当前格子的不同路径数,用二维数组dp保存。
更新当前点dp[i][j]为上一行同列dp[i-1][j]的值 + 本行上一列dp[i][j-1]的值,因为走到上一行同列的值想走到当前格都是往下走一步,左边同理。
初始化是第一行和第一列都是1.
答案是dp[m-1][n-1].
Time Complexity: O(m*n). Space: O(m*n).
AC Java:
1 public class Solution { 2 public int uniquePaths(int m, int n) { 3 if(m == 0 || n == 0){ 4 return 0; 5 } 6 int [][] dp = new int[m][n]; 7 for(int i = 0; i<m; i++){ 8 dp[i][0] = 1; 9 } 10 for(int j = 0; j<n; j++){ 11 dp[0][j] = 1; 12 } 13 for(int i = 1; i<m; i++){ 14 for(int j = 1; j<n; j++){ 15 dp[i][j] = dp[i-1][j] + dp[i][j-1]; 16 } 17 } 18 return dp[m-1][n-1]; 19 } 20 }
存储历史信息可以用一维数组完成从而节省空间。生成一个长度为n的数组dp, 每次更新dp[j] += dp[j-1], dp[j-1]就是同行前一列的历史结果,dp[j]为更新前是同列上一行的结果,所以dp[j] += dp[j-1]就是更新后的结果。
Note:外层loop i 从0开始,dp[0] = 1, 相当于初始了第一列,所以i=0开始要初始第一行
Time Complexity: O(m*n). Space: O(n).
1 class Solution { 2 public int uniquePaths(int m, int n) { 3 if(m == 0 || n == 0){ 4 return 0; 5 } 6 7 int [] dp = new int[n]; 8 for(int j = 0; j<n; j++){ 9 dp[j] = 1; 10 } 11 12 for(int i = 1; i<m; i++){ 13 for(int j = 1; j<n; j++){ 14 dp[j] = dp[j] + dp[j-1]; 15 } 16 } 17 18 return dp[n-1]; 19 } 20 }
有进阶版题目Unique Paths II.
转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/4824960.html
LeetCode Unique Paths相关推荐
- LeetCode | Unique Paths I,II
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [leetcode]Unique Paths II
>这道题目和上一题基本是一样的.只需要检测现在正在处理的那个单位是不是不可通过的,如果是就直接让这个单元格=0,也就是没有路径可以到右下角就好了. 代码如下: public class Solu ...
- [Lintcode]115. Unique Paths II/[Leetcode]63. Unique Paths II
115. Unique Paths II/63. Unique Paths II 本题难度: Easy/Medium Topic: Dynamic Programming Description Fo ...
- leetcode 62, 63, 980. Unique Paths I, II, III | 62, 63, 980. 不同路径 I, II, III(暴力递归->傻缓存->动态规划)
62. Unique Paths https://leetcode.com/problems/unique-paths/ 注意本题只能向右 / 向上走. DP 问题,经典又熟悉. 暴力递归->傻 ...
- 【动态规划】LeetCode 63. Unique Paths II
LeetCode 63. Unique Paths II Solution1:我的答案 在哪里做过这题? class Solution { public:int uniquePathsWithObst ...
- 【动态规划】LeetCode 62. Unique Paths
LeetCode 62. Unique Paths Solution1:我的未能AC的答案 递归超时了!!! class Solution { public:int uniquePaths(int m ...
- LeetCode 1.Minimum Path Sum 2.Unique Paths I and II
大家好,我是刘天昊,快到端午节了,今天说两道动态规划的题目(话说动规真的挺难的) 当然这三题是一样的解体思路先看Unique Paths A robot is located at the top-l ...
- 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). ...
- Unique Paths II leetcode java
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
最新文章
- vue-router 中导航守卫问题
- Coins POJ - 1742(题解)
- Vue CLI 3.0脚手架如何在本地配置mock数据json
- datagridviewrow 行宽_C# Winform DataGridView实现行[Row]的上下移动…….. | 学步园
- 关于Android工程师转成vue的三两事儿(4)--webpack
- PHP九宫格翻牌抽奖,PHP 九宫格抽奖代码
- ● firewalld.service Loaded: not-found (Reason: No such file or directory)
- 2017VS2018年非工作日日期大集合
- 软件工程复习笔记 用例图
- 柯尼卡美能达c353改语言,柯尼卡美能达bizhub c353c253c203维修手册中文部分2.pdf
- 威纶通触摸屏与仪表通讯_详解通信威纶通触摸屏与英威腾变频器的使用
- 屌炸天的3D引擎OpenCASCADE的用法及案例
- 让我们努力的学习ruby吧
- 数论概论 第三章 勾股数组与单位圆
- vim 常用指令与设置
- linux信号量对mysql_MySQL 信号量semaphore 和 innodb_adaptive_hash_index
- tex模版wins版本在mac中字体不对应问题解决方案
- openssl生成证书linux,Linux下使用openssl生成证书
- 使用python-aiohttp搭建微信公众平台
- Window11 alt+tab键失效
热门文章
- 【客户下单】后台系统自动分单成功生成工单发送短信
- sql中如何统计各种零件的总数量_如何应用GOF设计模式中的构建者模式创建复合对象实例...
- 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(三)
- BCompare日志
- 从接触FPGA开始...
- python(matplotlib5)——Contours 等高线图
- win10+anaconda3在 安装后‘conda‘ 不是内部或外部命令,也不是可运行的程序
- 用栈实现括号匹配的检验
- 约数个数 (排列组合中的乘法原理)
- myeclipse创建java错误提示_myeclipse 遇到的一些问题及解决方案