[LeetCode] Unique Paths、Unique Paths II、Minimum Path Sum
来源:互联网 发布:淘宝客服两字名字 编辑:程序博客网 时间:2024/06/07 22:00
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.
class Solution {public: int uniquePaths(int m, int n) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<int> > grid(m,vector<int>(n,0)); for(int i=0;i<m;i++) grid[i][0]=1; for(int i=0;i<n;i++) grid[0][i]=1; for(int i=1;i<m;i++) for(int j=1;j<n;j++) grid[i][j]=grid[i-1][j]+grid[i][j-1]; return grid[m-1][n-1]; }};
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.
class Solution {public: int uniquePathsWithObstacles(vector<vector<int> > &grid) { // Start typing your C/C++ solution below // DO NOT write int main() function int m=grid.size(); if ( m==0 ) return 0; int n = grid[0].size(); for(int i=0;i<m;i++) assert(grid[i].size()==n); if ( grid[0][0]== 1 ) return 0; vector<vector<int> > path(m,vector<int>(n,0)); path[0][0]=1; for(int i=1;i<m;i++) { if ( grid[i][0]==0 && path[i-1][0]==1) path[i][0]=1; else path[i][0]=0; } for(int j=1;j<n;j++) { if (grid[0][j]==0 &&path[0][j-1]==1) path[0][j]=1; else path[0][j]=0; } for(int i=1;i<m;i++) { for(int j=1;j<n;j++) { if( grid[i][j]==1 ) path[i][j]=0; else { path[i][j]=path[i-1][j]+path[i][j-1]; } } } return path[m-1][n-1]; }};
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.
class Solution {public: int minPathSum(vector<vector<int> > &grid) { // Start typing your C/C++ solution below // DO NOT write int main() function int m=grid.size(); if ( m==0 ) return 0; int n = grid[0].size(); for(int i=0;i<m;i++) assert(grid[i].size()==n); vector<vector<int> > sum(m,vector<int>(n,0)); sum[0][0]=grid[0][0]; for(int i=1;i<m;i++) sum[i][0]=sum[i-1][0]+grid[i][0]; for(int i=1;i<n;i++) sum[0][i]=sum[0][i-1]+grid[0][i]; for(int i=1;i<m;i++) for(int j=1;j<n;j++) sum[i][j]=min(sum[i-1][j],sum[i][j-1])+grid[i][j]; return sum[m-1][n-1]; }};
- [LeetCode] Unique Paths、Unique Paths II、Minimum Path Sum
- leetcode Unique Paths & Unique Paths II & Minimum Path Sum
- Unique Paths I,II, Minimum Path Sum
- 【LeetCode】Unique Paths I && II && Minimum Path Sum
- 62. Unique Paths& 63. Unique Paths II &64. Minimum Path Sum
- LeetCode 64/62/63. Minimum Path Sum/ Unique Paths i, ii
- LeetCode 1.Minimum Path Sum 2.Unique Paths I and II
- LeetCode: Unique Paths II
- [Leetcode] Unique Paths II
- LeetCode : Unique Paths II
- [LeetCode] Unique Paths II
- [LeetCode]Unique Paths II
- [leetcode]Unique Paths II
- LeetCode-Unique Paths II
- [leetcode] Unique Paths II
- LeetCode - Unique Paths II
- LeetCode:Unique Paths II
- 【LeetCode】Unique Paths II
- svn 拉分支
- iOS 名片识别代码
- ERROR 1045: Access denied for user: 'root@localhost' (Using password: YES)
- 编程之美(二)求二叉树中两节点的最大距离
- 史蒂夫·乔布斯
- [LeetCode] Unique Paths、Unique Paths II、Minimum Path Sum
- Selenium中无法定位的元素的原因探讨记录
- 编程之美(三)队列中取最大操作数的问题
- MOVE+SUMO MOVE 产生地图时候出错
- 浅谈3D网游引擎变迁史 看国产游戏引擎趋势
- 如何在linux嵌入式开发板上开机自动运行用户应用程序
- android的消息推送机制
- Git版本控制使用方法入门教程
- 论书法艺术在包装设计中的运用