当前位置:网站首页>一个机器人位于一个 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 !
边栏推荐
- 16 -- remove invalid parentheses
- Scrapy framework (I): basic use
- New method of cross domain image measurement style relevance: paper interpretation and code practice
- PSS:你距離NMS-free+提點只有兩個卷積層 | 2021論文
- 熊市慢慢,Bit.Store提供稳定Staking产品助你穿越牛熊
- Numerical extension of 27es6
- Distributed session solution
- Logstash excludes specific files or folders from collecting report log data
- 泰山OFFICE技术讲座:第一难点是竖向定位
- 一场分销裂变活动,不止是发发朋友圈这么简单!
猜你喜欢

Google Earth Engine(GEE)——Export. image. The difference and mixing of toasset/todrive, correctly export classification sample data to asset assets and references

Redis Series 2: data persistence improves availability

域名绑定动态IP最佳实践

Mobile terminal click penetration
![[pygame Games] ce jeu](/img/3c/e573106ec91441a554cba18d5b2253.png)
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)

3.1 simple condition judgment

Problems encountered in vs compilation

Julia constructs diagonal matrix

郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮

等保三级密码复杂度是多少?多久更换一次?
随机推荐
Does polardb-x currently not support self-made database service Das?
logstash排除特定文件或文件夹不采集上报日志数据
Design of direct spread spectrum communication system based on FPGA (with main code)
Can polardb-x be accessed through the client of related tools as long as the client supporting JDBC / ODBC protocol is afraid?
Centos8 PostgreSQL initialization error: initdb: error: invalid locale settings; check LANG and LC_* environment
New method of cross domain image measurement style relevance: paper interpretation and code practice
ORM表关系及操作
郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
Use redis to automatically cancel orders within 30 minutes
Leetcode daily practice (longest substring without repeated characters)
泰山OFFICE技术讲座:第一难点是竖向定位
Luogu_ P1002 [noip2002 popularization group] crossing the river_ dp
What is the open source compatibility of the current version of polardb-x? mysql8?
一场分销裂变活动,不止是发发朋友圈这么简单!
带你认识图数据库性能和场景测试利器LDBC SNB
QT5 之信号与槽机制(信号与槽的基本介绍)
Beginner level Luogu 1 [sequence structure] problem list solution
PSS:你距离NMS-free+提点只有两个卷积层 | 2021论文
跨域图像的衡量新方式Style relevance:论文解读和代码实战
Four characteristics of transactions