当前位置:网站首页>剑指 Offer II 099. 最小路径之和-双百代码
剑指 Offer II 099. 最小路径之和-双百代码
2022-07-02 22:09:00 【Mr Gao】
剑指 Offer II 099. 最小路径之和
给定一个包含非负整数的 m x n 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
说明:一个机器人每次只能向下或者向右移动一步。
示例 1:
输入:grid = [[1,3,1],[1,5,1],[4,2,1]]
输出:7
解释:因为路径 1→3→1→1→1 的总和最小。
示例 2:
输入:grid = [[1,2,3],[4,5,6]]
输出:12
这题解题的重点在于知道,每个点由上面一个点或左面一个点得到
解题代码如下:
int minPathSum(int** grid, int gridSize, int* gridColSize){
int i,j;
int n=gridSize,m=gridColSize[0];
// printf("nm %d %d || ",n,m);
int k=fmin(m,n);
for(i=0;i<k;i++){
for(j=i;j<m;j++){
// printf("%d %d ",i,j);
if(j==0&&i==0){
continue;
}
if(i==0){
grid[i][j]=grid[i][j-1]+ grid[i][j];
}
else{
if(grid[i][j-1]<grid[i-1][j]){
grid[i][j]=grid[i][j]+grid[i][j-1];
}
else{
grid[i][j]=grid[i][j]+grid[i-1][j];
}
}
// printf("%d %d %d ",i,j,grid[i][j]);
}
for(j=i+1;j<n;j++){
if(i==0){
grid[j][i]=grid[j-1][i]+ grid[j][i];
}
else{
if(grid[j][i-1]<grid[j-1][i]){
grid[j][i]=grid[j][i]+grid[j][i-1];
}
else{
grid[j][i]=grid[j][i]+grid[j-1][i];
}
}
// printf("-%d %d %d " ,j,i,grid[j][i]);
}
}
return grid[n-1][m-1];
}
边栏推荐
- Zhong Xuegao responded that the product will not melt for 1 hour: it contains solid components and cannot melt into water
- Go four singleton modes
- LC173. 二叉搜索树迭代器
- Webrtc audio and video capture and playback examples and mediastream media stream analysis
- [Luogu p1541] tortoise chess [DP]
- Baidu AI Cloud - create a face recognition application
- Kubernetes uses the host name to allocate the pod on the specified node
- Boot actuator - Prometheus use
- uniapp微信登录返显用户名和头像
- [LeetCode] 多数元素【169】
猜你喜欢

世界环境日 | 周大福用心服务推动减碳环保

wait解决僵尸进程

Niuke network: maximum submatrix

悬镜安全在RSAC2022上斩获Global InfoSec Awards四项大奖

Baidu AI Cloud - create a face recognition application

數據分析學習記錄--用EXCEL完成簡單的單因素方差分析

`Usage of ${}`

AES高级加密协议的动机阐述

数据分析学习记录(二)---响应曲面法及Design-Expert的简单使用

MySQL reset password, forget password, reset root password, reset MySQL password
随机推荐
杰理之样机在多次触摸后会触发关机【篇】
[Solved] Splunk: Cannot get username when all users are selected“
P7072 [CSP-J2020] 直播获奖
Methods of adding styles to native JS
Oracle cursor
Data analysis learning records -- complete a simple one-way ANOVA with Excel
`${}`的用法
Niuke network: maximum submatrix
'when to use const char * and when to use const char []' - when to use const char * and when to use const char []
【板栗糖GIS】arcmap—如何批量修改注记要素的字体,颜色,大小等
PHP implements querying the data matching the date of birth according to the entered age
杰理之直接触摸样机的顶针反应不正常【篇】
Mask R-CNN
[chestnut sugar GIS] ArcMap - why should the tick of classic capture be removed when using custom capture?
Go condition variable
位的高阶运算
Local dealers play the community group purchase mode and share millions of operations
傑理之修改不需要長按開機功能【篇】
中国信通院、清华大学、腾讯安全,云原生安全产学研用强强联合!
To myself who is about to work