[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). ...
随机推荐
- Eclipse 使用 VS Emulator for android 调试环境配置 步骤
模拟器启动器地址:C:\Program Files (x86)\Microsoft Emulator Manager\1.0\emulatorcmd.exe 获取模拟器ID命令:emulatorcmd ...
- ipc基础
ipc对象持久性 进程持久性:具有这种持久性的对象在持有它的最后一个进程关闭了该对象为止 内核持久性:这种IPC对象一直存在直到内核重新自举或显示删除该对象为止 文件系统持久性:具有这种持久性的对象只 ...
- Tomcat整体架构分析
下面让我们来看看Tomcat容器的整体结构: 本文的目的是覆盖这张图中所涉及的主要请求处理组件.而上图中的一些高级主题如集群和安全则不是在本文讨论的范围之内. 本图中,Service, Host, C ...
- python网页爬虫开发之四-串行爬虫代码示例
实现功能:代理.限速.深度.反爬 import re import queue import urllib.parse import urllib.robotparser import time fr ...
- Let'sencrypt.sh 抛出异常: Response: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726)>
起因 今天网站的SSL证书过期了,打算重新申请,运行 Let'sencrypt.sh 的时候抛出了这么个异常. 一番搜索,发现居然找不到直接的答案.没有直接的答案就只能通过间接的答案来解决了. 希望我 ...
- Vue 目录结构 绑定数据 绑定属性 循环渲染数据
一.目录结构分析 node_modules 项目所需要的各种依赖 src 开发用的资源 assets 静态资源文件 App.vue 根组件 main.js 配置路由时会用 .babelrc 配置文件 ...
- Android Studio2.0 教程从入门到精通Windows版
系列教程 Android Studio2.0 教程从入门到精通Windows版 - 安装篇Android Studio2.0 教程从入门到精通Windows版 - 入门篇Android Studio2 ...
- android 开发 View _12_ 用Canvas 绘制一张图片(博客中演示用Canvas画验证码图片)
package net.yt.yuncare.widgets; import android.graphics.Bitmap; import android.graphics.Canvas; impo ...
- python实战博客
2018-10-31 更新Logging日志记录以及异常捕获 感谢廖大教程.Python实战 直接在闲置的服务器上开发.阿里云Centos 6.8 64位. 1 搭建开发环境 Python 环境是Py ...
- HDFS 常用命令行:
1. 查看各库的存储大小 hdfs dfs -du -h /user/hive/warehouse 2. 删除HDFS 文件 hdfs dfs -rmr 绝对路径名 例如:hdfs dfs -rmr ...