题目
https://www.nowcoder.com/practice/7d21b6be4c6b429bb92d219341c4f8bb?tpId=190&&tqId=35224&rp=1&ru=/ta/job-code-high-rd&qru=/ta/job-code-high-rd/question-ranking
import java.util.*;
public class Solution {
/**
*
* @param matrix int整型二维数组 the matrix
* @return int整型
*/
public static int minPathSum (int[][] matrix) {
if (matrix.length == 0){
return 0;
}
for (int i = 1 ;i < matrix[0].length ; i++){
matrix[0][i] += matrix[0][i-1];
}
for (int i = 1; i < matrix.length ; i++){
matrix[i][0] += matrix[i-1][0];
}
for (int i = 1 ; i < matrix.length ; i++){
for (int j = 1 ; j < matrix[i].length ; j++){
matrix[i][j] += Math.min(matrix[i][j-1],matrix[i-1][j]);
}
}
return matrix[matrix.length-1][matrix[0].length-1];
}
}
全部评论
(2) 回帖