当前位置:网站首页>剑指 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];
}
边栏推荐
- [chestnut sugar GIS] how does global mapper batch produce ground contour lines through DSM
- 首批 | 腾讯云完成国内首个云原生安全成熟度评估
- 电路设计者常用的学习网站
- [leetcode] most elements [169]
- uniapp微信登录返显用户名和头像
- How should programmers write logs
- Dahua cloud native load balancing article - the passenger flow of small restaurants has increased
- Go four singleton modes
- 杰理之内置关机电流 1.2uA,之后不能长按开机【篇】
- Array advanced improvement
猜你喜欢
[leetcode] most elements [169]
[error record] the flutter reports an error (could not read script 'xxx\flutter\u tools\gradle\app\u plugin\u loader.gradle')
中国信通院、清华大学、腾讯安全,云原生安全产学研用强强联合!
创新实力再获认可!腾讯安全MSS获2022年度云原生安全守护先锋
[羊城杯2020]easyphp
[LeetCode] 数组中的第K个最大元素【215】
Niuke network: maximum submatrix
牛客网:龙与地下城游戏
PMP项目整合管理
Hanging mirror security won four global infosec awards on rsac2022
随机推荐
【板栗糖GIS】global mapper 如何通过dsm批量制作贴地等高线
Jielizhi, production line assembly link [chapter]
Solve the error of changing the selected file when uploading excel file. Net:: err_ UPLOAD_ FILE_ CHANGED
Methods of adding styles to native JS
Splunk audit 的设定
大话云原生之负载均衡篇-小饭馆客流量变大了
Jerry's charge unplugged, unable to touch the boot [chapter]
E-commerce system microservice architecture
Niuke: Dragon and dungeon games
Graphic view frame
Dahua cloud native load balancing article - the passenger flow of small restaurants has increased
easyclick,ec权朗网络验证源码
antd组件upload上传xlsx文件,并读取文件内容
分享 10 个 JS 闭包面试题(图解),进来看看你能答对多少
Learning records of data analysis (II) -- simple use of response surface method and design expert
电商系统微服务架构
Zhong Xuegao responded that the product will not melt for 1 hour: it contains solid components and cannot melt into water
Data analysis learning records -- complete a simple one-way ANOVA with Excel
大一学习分享
[LeetCode] 存在重复元素【217】