# 最小路径和

<p>给定一个包含非负整数的 <code><em>m</em> x <em>n</em></code> 网格 <code>grid</code> ，请找出一条从左上角到右下角的路径，使得路径上的数字总和为最小。</p><p><strong>说明：</strong>每次只能向下或者向右移动一步。</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/0c6d9496445c66b255a406ba2f9a3eb9.jpeg" style="width: 242px; height: 242px;" /><pre><strong>输入：</strong>grid = [[1,3,1],[1,5,1],[4,2,1]]<strong><br />输出：</strong>7<strong><br />解释：</strong>因为路径 1→3→1→1→1 的总和最小。</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>grid = [[1,2,3],[4,5,6]]<strong><br />输出：</strong>12</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>m == grid.length</code></li>	<li><code>n == grid[i].length</code></li>	<li><code>1 <= m, n <= 200</code></li>	<li><code>0 <= grid[i][j] <= 100</code></li></ul>

## template

```java
class Solution {
	public int minPathSum(int[][] grid) {
		int m = grid.length;
		int n = grid[0].length;

		int sum = 0;

		if (m < 1 || n < 1)
			return 0;
		if (m == 1) {
			for (int i = 0; i < n; i++) {
				sum = sum + grid[0][i];
			}
			return sum;
		}
		if (n == 1) {
			for (int i = 0; i < m; i++) {
				sum = sum + grid[i][0];
			}
			return sum;
		}

		int[][] dp = new int[m][n];
		dp[0][0] = grid[0][0];

		for (int k = 1; k < m; k++) {
			dp[k][0] = grid[k][0] + dp[k - 1][0];
		}

		for (int l = 1; l < n; l++) {
			dp[0][l] = grid[0][l] + dp[0][l - 1];
		}

		for (int k = 1; k < m; k++) {
			for (int l = 1; l < n; l++) {
				dp[k][l] = grid[k][l] + Math.min(dp[k - 1][l], dp[k][l - 1]);
			}
		}
		return dp[m - 1][n - 1];
	}
}

```

## 答案

```java

```

## 选项

### A

```java

```

### B

```java

```

### C

```java

```