当前位置:网站首页>一个机器人位于一个 m x n 网格的左上角 。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角。问总共有多少条不同的路径?【LeetCodeHot100】
一个机器人位于一个 m x n 网格的左上角 。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角。问总共有多少条不同的路径?【LeetCodeHot100】
2022-06-27 15:41:00 【小型骷髅】
力扣热题100之62:

先贴代码:
class Solution {
public int uniquePaths(int m, int n) {
// 创建棋盘
int[][] board = new int[m][n];
// 将第0列的格子路径设为1
for (int i = 0; i < m; i++) {
board[i][0] = 1;
}
// 将第0行的格子路径设为1
for (int j = 0; j < n; j++) {
board[0][j] = 1;
}
// 往后累加格子的路径数
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
board[i][j] = board[i-1][j] + board[i][j-1];
}
}
return board[m-1][n-1];
}
}解题思路:
题目中告诉我们,需要抵达棋盘的终点(右下角),并且机器人只能一次向右或者向下移动一步,那么当我们抵达一个格子时,只能是从它的左边或者上边过来,由此我们可以推断出:
抵达一个格子的路径数 = 抵达这个格子左边格子的路径数 + 抵达这个格子上边格子的路径数;
即表达式为: f( i , j ) = f( i - 1 , j ) + f( i , j - 1 );
我们先将第0行和第0列的格子全部设为1,表示从起始点走到这些格子有且只有一条路径可以走,我们拿一个 3 * 6 的棋盘举例:
再来开始依次计算剩下空白的格子的路径数,因为由上可知表达式:
f( i , j ) = f( i - 1 , j ) + f( i , j - 1 );
所以在求一个格子的路径数时,只要把左边格子 + 上边格子 即可。求得剩下格子路径数为:

由此就可以得出 到达终点的路径数为 15 + 6 = 21 !
边栏推荐
- Slow bear market, bit Store provides stable stacking products to help you cross the bull and bear
- Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration
- 数据中心表格报表实现定制统计加班请假汇总记录分享
- Scrapy framework (I): basic use
- PSS: you are only two convolution layers away from the NMS free+ point | 2021 paper
- 目前PolarDB-X是不支持数据库自制服务DAS么?
- 鸿蒙发力!HDD杭州站·线下沙龙邀您共建生态
- 开源二三事|ShardingSphere 与 Database Mesh 之间不得不说的那些事
- LeetCode每日一练(无重复字符的最长子串)
- Google Earth Engine(GEE)——Export. image. The difference and mixing of toasset/todrive, correctly export classification sample data to asset assets and references
猜你喜欢

IDE Eval reset unlimited trial reset

PSS:你距離NMS-free+提點只有兩個卷積層 | 2021論文

Source NAT address translation and server mapping web page configuration of firewall Foundation

LeetCode每日一练(杨辉三角)

3.4 fixed number of cycles II

Open source 23 things shardingsphere and database mesh have to say

express

LeetCode每日一练(无重复字符的最长子串)

ICML 2022 | 阿⾥达摩院最新FEDformer,⻓程时序预测全⾯超越SOTA

熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
随机推荐
If you want to use DMS to handle database permissions, can you only use Alibaba cloud ram accounts (Alibaba cloud RDS)
IDE Eval reset unlimited trial reset
FPGA based analog I ² C protocol system design (with main code)
关于快速幂
Taishan Office Technology Lecture: the first difficulty is vertical positioning
List转Table
ICML 2022 | 阿⾥达摩院最新FEDformer,⻓程时序预测全⾯超越SOTA
VS编译遇到的问题
The role of the symbol @ in MySQL
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)
防火墙基础之源NAT地址转换和服务器映射web页面配置
[170] the PostgreSQL 10 field type is changed from string to integer, and the error column cannot be cast automatically to type integer is reported
Design of spread spectrum communication system based on FPGA (with main code)
Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology
Leetcode daily practice (main elements)
16 -- remove invalid parentheses
Numerical extension of 27es6
[kotlin] the next day
Cesium 使用MediaStreamRecorder 或者MediaRecorder录屏并下载视频,以及开启摄像头录像。【转】
[pyGame games] this "eat everything" game is really wonderful? Eat them all? (with source code for free)