国产探花免费观看_亚洲丰满少妇自慰呻吟_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]; }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西乌珠穆沁旗| 谢通门县| 蒲江县| 延川县| 额尔古纳市| 中超| 云龙县| 广河县| 洛扎县| 正宁县| 仁布县| 罗源县| 工布江达县| 鄱阳县| 汉中市| 左贡县| 涞源县| 广丰县| 平果县| 池州市| 永丰县| 安岳县| 内江市| 田阳县| 岳西县| 延安市| 上饶县| 芒康县| 友谊县| 彝良县| 杭锦后旗| 西吉县| 赣州市| 汪清县| 建宁县| 黎城县| 崇明县| 安国市| 融水| 灵台县| 沂水县|