注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
最近等offer 于是准备陆陆续续开始刷题
遇到过好几次这种情况了,就是LeetCode在线的输出和本地eclipse输出不一样。。
比如LeetCode 64
测试案例是[1,2,5],[3,2,1] 本来正确的输出是6 然后我在我电脑上跑也是6 然后在人家在线那上面跑就是5。。。
然后我debug了下发现自己eclipse上过程是按照我想的呀,结果也是6。。。 而且我看我的思路跟disscuss上的也一样啊。。。
求大神看看到底是哪里出了问题呀?
我代码功底不行,大家想说啥说啥。。。
class Solution {
public int minPathSum(int[][] grid) {
if(grid.length==0)
return 0;
if(grid.length==2){
int a=grid[0][0]+grid[0][1]+grid[1][1];
int b=grid[1][0]+grid[0][0]+grid[1][1];
return a>b?b:a;
}
int m=grid.length;
int n=grid[0].length;
int cost[][]=new int[m][n];
cost[0][0]=grid[0][0];
for(int i=1;i<m;i++)
cost[i][0]=grid[i][0]+cost[i-1][0];
for(int j=1;j<n;j++)
cost[0][j]=grid[0][j]+cost[0][j-1];
for(int i=1;i<m;i++){
for(int j=1;j<n;j++){
if(cost[i-1][j]<cost[i][j-1])
cost[i][j]=grid[i][j]+cost[i-1][j];
else
cost[i][j]=grid[i][j]+cost[i][j-1];
}
}
return cost[m-1][n-1];
}
}
|