当前位置:网站首页>力扣:62.不同路径
力扣:62.不同路径
2022-08-04 05:14:00 【empty__barrel】
力扣:62.不同路径
题目:
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。
问总共有多少条不同的路径?
代码:dp一题比代码随想录空间利用率高
class Solution {
public:
int uniquePaths(int m, int n) {
int dp[m][n];
// dp[0][0] = 0;
for(int i = 0; i < m; ++i){
for(int j = 0; j < n; ++j){
if(i== 0 && j==0) {
dp[0][0] = 1;
continue;
}
int a = j-1 < 0? 0:dp[i][j-1];
int b = i-1 < 0? 0:dp[i-1][j];
dp[i][j] = a+b;
}
}
return dp[m-1][n-1];
}
};
代码随想录代码:
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> dp(m, vector<int>(n, 0));
for (int i = 0; i < m; i++) dp[i][0] = 1;
for (int j = 0; j < n; j++) dp[0][j] = 1;
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
};
代码:一维数组的实现
class Solution {
public:
int uniquePaths(int m, int n) {
vector<int> dp(n);
for (int i = 0; i < n; i++) dp[i] = 1;
for (int j = 1; j < m; j++) {
for (int i = 1; i < n; i++) {
dp[i] += dp[i - 1];
}
}
return dp[n - 1];
}
};
代码:数论方法
无论怎么走,走到终点都需要 m + n - 2 步,在这m + n - 2 步中,一定有 m - 1 步是要向下走的,此时就是一个组合问题了,判断m-1步是哪几步即可。
求组合的时候,要防止两个int相乘溢出! 所以不能把算式的分子都算出来,分母都算出来再做除法。需要在计算分子的时候,不断除以分母,
class Solution {
public:
int uniquePaths(int m, int n) {
long long numerator = 1; // 分子
int denominator = m - 1; // 分母
int count = m - 1;
int t = m + n - 2;
while (count--) {
numerator *= (t--);
while (denominator != 0 && numerator % denominator == 0) {
numerator /= denominator;
denominator--;
}
}
return numerator;
}
};
边栏推荐
- 【21天学习挑战赛】图像的旋转问题(二维数组)
- drools from download to postman request success
- Turn: Management is the love of possibility, and managers must have the courage to break into the unknown
- 一个对象引用的思考
- C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.3 什么是声明,什么是定义
- 小程序 + 电商,玩转新零售
- C专家编程 第5章 对链接的思考 5.2 动态链接的优点
- 【21天学习挑战赛】顺序查找
- About yolo7 and gpu
- C Expert Programming Chapter 4 The Shocking Fact: Arrays and pointers are not the same 4.2 Why does my code not work
猜你喜欢

System design. How to design a spike system (full version transfer)

px、em、rem的区别

docker安装mysql与宿主机相差8小时的问题。

Typora 使用保姆级教程 | 看这一篇就够了 | 历史版本已被禁用

7-1 LVS+NAT load balancing cluster, NAT mode deployment

manipulation of file contents

Towards Real-Time Multi-Object Tracking (JDE)

读者让我总结一波 redis 面试题,现在肝出来了

【云原生--Kubernetes】Pod资源管理与探针检测

What is the salary of a software testing student?
随机推荐
C专家编程 第5章 对链接的思考 5.6 轻松一下---看看谁在说话:挑战Turning测验
7-1 LVS+NAT load balancing cluster, NAT mode deployment
3面头条,花7天整理了面试题和学习笔记,已正式入职半个月
C专家编程 第4章 令人震惊的事实:数组和指针并不相同 4.4 使声明与定义相匹配
字节最爱问的智力题,你会几道?
7-2 LVS+DR Overview and Deployment
leetcode 12. 整数转罗马数字
mysql index notes
The difference between px, em, and rem
static在不同位置定义变量居然还有不同的含义?
C Expert Programming Chapter 5 Thinking about Linking 5.1 Libraries, Linking and Loading
离线采集怎么看sql执行计划
Get the selected content of the radio box
go module的介绍与应用
There is an 8 hour difference between the docker installation of mysql and the host.
flink cdc一启动,源端Oracle那台服务器的CPU就飙升到80%以上,会是啥原因呢?
OpenSSF 安全计划:SBOM 将驱动软件供应链安全
震惊,99.9% 的同学没有真正理解字符串的不可变性
Use Patroni callback script to bind VIP pit
看DevExpress丰富图表样式,如何为基金公司业务创新赋能