国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

62. Unique Paths 和63. Unique Paths II

2019-11-08 03:10:43
字體:
來源:轉載
供稿:網友

一、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. 方法一、數學方法:

int uniquePaths(int m, int n) { int N = m + n -2; int k = m - 1; double res = 1; for(int i = 1; i <= k; i++) res = res * (N - k + i) / i; return (int)res; }

方法二、動態規劃的方法:

int uniquePaths(int m, int n) { vector<vector<int>> path(m,vector<int> (n,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]; }

二、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. 動態規劃:

int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) { int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); vector<vector<int>> dp(m+1,vector<int>(n+1,0)); dp[0][1] = 1; for(int i = 1; i <= m; i++){ for(int j = 1; j <= n; j++) if(!obstacleGrid[i-1][j-1]) dp[i][j] = dp[i-1][j] + dp[i][j-1]; } return dp[m][n]; }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 渑池县| 永修县| 罗城| 潮安县| 旌德县| 邢台县| 平罗县| 鹤壁市| 耒阳市| 米泉市| 万源市| 哈尔滨市| 类乌齐县| 南投县| 岱山县| 株洲县| 岗巴县| 望谟县| 凤冈县| 磐安县| 平潭县| 吉木萨尔县| 波密县| 临江市| 错那县| 灵寿县| 吴川市| 长顺县| 文登市| 化州市| 南部县| 柳江县| 顺义区| 河东区| 噶尔县| 磴口县| 海口市| 临洮县| 桂东县| 南华县| 公主岭市|