您的位置:首页 > 产品设计 > UI/UE

leetcode解题之62&63. Unique Paths ||64. Minimum Path Sum java版(路径(最短)可达)

2017-04-10 11:14 609 查看

62. Unique Paths

A robot is located at the top-left corner of a
m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?



Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.

设path[i][j] 为从起点到(i,j)位置处的路径数。

第一行,第一列都为1(只能一个方向行走)

到其他位置处(i,j):到达位置(i,j)只能从上面或者左面过来,因此决定到位置(i,j)的路径数由到达上面位置(i-1,j)的路径数和到达左面位置(i,j-1)的路径所决定的。

状态转移方程:

path[i][j] =path[i-1][j] + path[i][j-1]

时间复杂度:O(n^2)  空间复杂度:O(n^2)

public int uniquePaths(int m, int n) {
int[][] path = new int[m]
;
for (int i = 0; i < m; i++)
path[i][0] = 1;
for (int i = 0; i < n; i++)
path[0][i] = 1;
for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++)
path[i][j] = path[i - 1][j] + path[i][j - 1];
System.out.println(path[m-1][n-1]);
return path[m-1][n-1];
}想到用DFS+回溯来做但是超时!
int count;
public int uniquePath(int m, int n) {
boolean[][] visited = new boolean[m]
;
dfsCore(visited, m, n, 0, 0);
//System.out.println(count);
return count;
}

private void dfsCore(boolean[][] visited, int m, int n, int row, int col) {
if (row == m - 1 && col == n - 1) {
count++;
return;
}
if (col >= n || row >= m)
return;
if (visited[row][col])
return;
visited[row][col] = true;
dfsCore(visited, m, n, row + 1, col);
dfsCore(visited, m, n, row, col + 1);
visited[row][col] = false;
}

63. Unique Paths II

Follow up for "Unique Paths":

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as
1
and
0
respectively in the grid.

For example,

There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
[0,0,0],
[0,1,0],
[0,0,0]
]

The total number of unique paths is
2
.

Note: m and n will be at most 100.

有障碍物,和上题一样,为1,那么此时path[i][j]为0,第一行和第一列注意初始化;

public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int m = obstacleGrid.length;
int n = obstacleGrid[0].length;
int[][] path = new int[m]
;
// 第一列行初始化,只能从上到下
for (int i = 0; i < m; i++) {
// 如果有一个为1,那么剩下的第一行全部不可达
if (obstacleGrid[i][0] == 1)
break;
path[i][0] = 1;
}
// 第一行行初始化,只能从左到右
for (int i = 0; i < n; i++) {
// 如果有一个为1,那么剩下的第一行全部不可达
if (obstacleGrid[0][i] == 1)
break;
// 默认值为0
path[0][i] = 1;
}
for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++) {
path[i][j] = path[i - 1][j] + path[i][j - 1];
if (obstacleGrid[i][j] == 1)
path[i][j] = 0;
}
// System.out.println(path[m - 1][n - 1]);
return path[m - 1][n - 1];
}

64. Minimum Path Sum(最小路径代价)

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which
minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

先处理最左边和最上边两条边,因为只有一条路。接下来每一点的值等于它上边和左边的较小值加上该点的数值~即为到达该点的最短路径

public int minPathSum(int[][] grid) {
if (grid == null || grid.length == 0)
return 0;
int m = grid.length;
int n = grid[0].length;
int[][] dp = new int[m]
;

dp[0][0] = grid[0][0];

for (int i = 0; i < n - 1; i++)
dp[0][i + 1] = dp[0][i] + grid[0][i + 1];
for (int i = 0; i < m - 1; i++)
dp[i + 1][0] = dp[i][0] + grid[i + 1][0];

for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++)
dp[i][j] = Math.min(dp[i - 1][j],
dp[i][j - 1]) + grid[i][j];
return dp[m - 1][n - 1];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息