当前位置:网站首页>算法---不同路径(Kotlin)
算法---不同路径(Kotlin)
2022-07-28 12:46:00 【小米科技Android 研发曹新雨】
题目
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。
问总共有多少条不同的路径?
示例 :
输入:m = 3, n = 2
输出:3
解释:
从左上角开始,总共有 3 条路径可以到达右下角。
- 向右 -> 向下 -> 向下
- 向下 -> 向下 -> 向右
- 向下 -> 向右 -> 向下
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/unique-paths
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解决思路

解决方法
fun uniquePaths(m: Int, n: Int): Int {
val dp = Array(m) {
Array(n) {
0 } }
dp[0][0] = 1
for (i in 0 until m) {
for (j in 0 until n) {
if (i == 0 && j ==0) continue
val left = if (i - 1 >= 0) dp[i - 1][j] else 0
val top = if (j - 1 >= 0) dp[i][j - 1] else 0
dp[i][j] = left + top
println(" $i $j ${
dp[i][j]}")
}
}
return dp[m - 1][n - 1]
}
总结
1.别人眼中最简单的,到我这里一直没有思路。看了题解才知道思路是什么。
边栏推荐
- 【架构】评分较高的三本微服务书籍的阅读笔记
- Can second uncle cure young people's spiritual internal friction?
- 力扣 剑指 Offer 51. 数组中的逆序对
- POJ3268最短路径题解
- R语言ggplot2可视化:可视化散点图并为散点图中的数据点添加文本标签、使用ggrepel包的geom_text_repel函数避免数据点标签互相重叠(自定义指定字体类型font family)
- Guide for using IP phone system and VoIP system
- Shell basic concepts and variables
- 111. SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误
- R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的边框颜色
- [ecmascript6] function and its related use
猜你喜欢

How to check if the interface cannot be adjusted? I didn't expect that the old bird of the 10-year test was planted on this interview question

111. The sap ui5 fileuploader control realizes local file upload and encounters a cross domain access error when receiving the response from the server

Go language - Application of stack - expression evaluation
![[dark horse morning post] byte valuation has shrunk to $270billion;](/img/58/8d5c78d919ed60bc833ec4daa22e23.jpg)
[dark horse morning post] byte valuation has shrunk to $270billion; "Second uncle" video author responded to plagiarism; Renzeping said that the abolition of the pre-sale system of commercial housing

Intra prediction and transform kernel selection based on Neural Network

《暗黑破坏神4》PS4/PS5测试版已加入PlayStation数据库

SQL daily practice (Niuke new question bank) - day 4: advanced operators

Three men "running away" from high positions in the mobile phone factory

SQL每日一练(牛客新题库)——第4天:高级操作符

Customized template in wechat applet
随机推荐
今日睡眠质量记录75分
Intra prediction and transform kernel selection based on Neural Network
【架构】评分较高的三本微服务书籍的阅读笔记
C语言:随机生成数+归并排序
使用 Fail2ban 保护 Web 服务器免受 DDoS 攻击
[apue] 文件中的空洞
The domestic API management tool eolink is very easy to use, creating an efficient research and development tool
二舅能治好年轻人的精神内耗吗?
数据库系统原理与应用教程(058)—— MySQL 练习题(二):单选题
[Architecture] reading notes of three micro service books with high scores
微念“失去”李子柒的这一年
沾上趣店,都得道歉?
R语言使用dpois函数生成泊松分布密度数据、使用plot函数可视化泊松分布密度数据(Poisson distribution)
长封闭期私募产品再现 业内人士看法各异
《暗黑破坏神4》PS4/PS5测试版已加入PlayStation数据库
Night God simulator packet capturing wechat applet
How to check if the interface cannot be adjusted? I didn't expect that the old bird of the 10-year test was planted on this interview question
Blue Bridge Training (additional interview questions) day 7
从手机厂高位“出走”的三个男人
Jenkins--持续集成服务器