LeetCode的medium题集合(C++实现)十一
2015-05-23 10:16
363 查看
1 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?
因为只能向右和向下移动,所以在(i,j)(i,j)位置的上一个位置只能是(i−1,j)(i-1,j)或(i,j−1)(i,j-1),用path[i][j]path[i][j]表示从开始位置到当前位置的路径和,则有path[i][j]=path[i−1][j]+path[i][j−1]path[i][j]=path[i-1][j]+path[i][j-1]
利用上一轮的计算结果,可以将二维数组降为一维
2 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.
与上一题一样,用path[i][j]path[i][j]表示从开始位置到当前位置的路径和。区别是要判断是否有障碍,如果有这将path[i][j]path[i][j]置0,没障碍时则有path[i][j]=path[i−1][j]+path[i][j−1]path[i][j]=path[i-1][j]+path[i][j-1]
3 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.
与第一题方法相似
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?
因为只能向右和向下移动,所以在(i,j)(i,j)位置的上一个位置只能是(i−1,j)(i-1,j)或(i,j−1)(i,j-1),用path[i][j]path[i][j]表示从开始位置到当前位置的路径和,则有path[i][j]=path[i−1][j]+path[i][j−1]path[i][j]=path[i-1][j]+path[i][j-1]
[code]int uniquePaths(int m, int n) { vector<vector<int> > path(m,vector<int>(n,0)); //初始化顶部和左边的格子 for(int i=0;i<m;i++) path[i][0]=1; for(int j=0;j<n;j++) path[0][j]=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]; } } return path[m-1][n-1]; }
利用上一轮的计算结果,可以将二维数组降为一维
[code]int uniquePaths(int m, int n) { vector<int> path(n,0); for(int j=0;j<n;j++) path[j]=1; for(int i=1;i<m;i++) { for(int j=1;j<n;j++) { path[j]=path[j]+path[j-1]; } } return path[n-1]; }
2 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.
与上一题一样,用path[i][j]path[i][j]表示从开始位置到当前位置的路径和。区别是要判断是否有障碍,如果有这将path[i][j]path[i][j]置0,没障碍时则有path[i][j]=path[i−1][j]+path[i][j−1]path[i][j]=path[i-1][j]+path[i][j-1]
[code]int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int m=obstacleGrid.size(); int n=obstacleGrid[0].size(); vector<vector<int> > path(m,vector<int>(n,0)); path[0][0]=obstacleGrid[0][0]==1? 0:1; for(int i=1;i<m;i++) path[i][0]=obstacleGrid[i][0]==1? 0:path[i-1][0]; for(int j=1;j<n;j++) path[0][j]=obstacleGrid[0][j]==1? 0:path[0][j-1]; for(int i=1;i<m;i++) { for(int j=1;j<n;j++) { path[i][j]=obstacleGrid[i][j]==1? 0:(path[i-1][j]+path[i][j-1]); } } return path[m-1][n-1]; }
3 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.
与第一题方法相似
[code]int minPathSum(vector<vector<int>>& grid) { int m=grid.size(); int n=grid[0].size(); vector<vector<int> > path(m,vector<int>(n,0)); path[0][0]=grid[0][0]; for(int i=1;i<m;i++) path[i][0]=path[i-1][0]+grid[i][0]; for(int j=1;j<n;j++) path[0][j]=path[0][j-1]+grid[0][j]; for(int i=1;i<m;i++) { for(int j=1;j<n;j++) { path[i][j]=grid[i][j]+min(path[i-1][j],path[i][j-1]); } } return path[m-1][n-1]; }
相关文章推荐
- LeetCode的medium题集合(C++实现)十五
- LeetCode的medium题集合(C++实现)十四
- LeetCode的medium题集合(C++实现)一
- LeetCode的medium题集合(C++实现)五
- LeetCode的medium题集合(C++实现)七
- LeetCode的medium题集合(C++实现)三
- LeetCode的medium题集合(C++实现)九
- LeetCode的medium题集合(C++实现)四
- LeetCode的medium题集合(C++实现)十三
- LeetCode的medium题集合(C++实现)十
- LeetCode的medium题集合(C++实现)十二
- LeetCode的medium题集合(C++实现)八
- LeetCode的medium题集合(C++实现)六
- LeetCode的medium题集合(C++实现)十六
- LeetCode的medium题集合(C++实现)十七
- LeetCode的medium题集合(C++实现)二
- LeetCode的easy题集合(C++实现)四
- [leetcode]55. Jump Game ,C++/PYTHON实现,medium难度
- LeetCode的easy题集合(C++实现)三
- LeetCode的easy题集合(C++实现)二