Skip to main content

🟡 剑指 Offer II 099. 最小路径之和

LeetCode 提示

题目难度 中等

原题链接 🔗 leetcode

题解1#

class Solution {    public int minPathSum(int[][] grid) {        int yy=grid.length, xx=grid[0].length;        int[][] sums = new int[2][xx+1];        int si = 0;
        for (int i=0; i<xx; i++) {            sums[si][i+1] = grid[0][i] + sums[si][i];        }        sums[0][0] = Integer.MAX_VALUE;        sums[1][0] = Integer.MAX_VALUE;
        for (int j=1; j<yy; j++) {            si = 1-si;            for (int i=0; i<xx; i++) {                sums[si][i+1] = grid[j][i] + Math.min(sums[si][i], sums[1-si][i+1]);            }        }
        return sums[si][xx];    }}