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

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

62. Unique Paths / 63. Unique Paths II

2019-11-10 18:47:29
字體:
供稿:網(wǎng)友

Unique Paths題目描述解法描述Unique Paths II題目描述實(shí)現(xiàn)代碼

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?

一個(gè)矩陣mxn,從左上角到右下角有多少條路徑。

解法描述

法一:使用深度搜索。這種方法會(huì)超時(shí)。

class Solution {public: void uniquePathsNum(int &res, int m, int n, int i, int j, int stt) { if(i > m || j > n) return; if(stt >= m+n - 2) { if(m == i && n == j) {res++; return;} else return; } uniquePathsNum(res, m, n, i+1, j, stt+1); uniquePathsNum(res, m, n, i, j+1, stt+1); } int uniquePaths(int m, int n) { int res = 0; uniquePathsNum(res, m, n, 1, 1, 0); return res; }};

法二:使用DP來計(jì)算。

class Solution {public: int uniquePaths(int m, int n) { /* int **tmp = new int*[m+1]; for(int k = 0; k <= m; k++) tmp[k] = new int[n+1]; */ int tmp[101][101] = {0}; tmp[1][1] = 1; // cout << tmp[0][1] << endl; for(int i = 1; i <= m; i++) { for(int j = 1; j <= n; j++) { tmp[i][j] += (tmp[i-1][j] + tmp[i][j-1]); // cout << i << " " << j << " " << tmp[i][j] << " " << tmp[i-1][j] << " " << tmp[i][j-1] << endl; } } int res = tmp[m][n]; /* for(int i = 0; i <= m; i++) delete tmp[i]; delete tmp; */ return res; }};

使用空間復(fù)雜度O(n)的代碼:

class Solution {public: int uniquePaths(int m, int n) { int *memo = new int[n]; for(int i = 0; i < n; i++) memo[i] = 1; for(int i = 1 ; i < m; i++) for(int j = 1; j < n; j++) memo[j] += memo[j-1]; return memo[n-1]; }};

當(dāng)然這道題還有方法,那就是使用高中學(xué)過的排列組合。

class Solution {public: int uniquePaths(int m, int n) { if(m == 1 || n == 1) return 1; m--; n--; if(m < n) { // Swap, so that m is the bigger number m = m + n; n = m - n; m = m - n; } long res = 1; int j = 1; for(int i = m+1; i <= m+n; i++, j++){ // Instead of taking factorial, keep on multiply & divide res *= i; res /= j; } return (int)res; }};class Solution {public: int uniquePaths(int m, int n) { return nCr (m + n - 2, min(m, n) - 1); }PRivate: int nCr (int n, int r) { long long_result = 1; for (int i = 0; i != r; ++i) { // from n - r + 1 (when i = 0) to n (when i = r - 1) long_result *= (n - r + 1 + i); // from 1 (when i = 0) to r (when i = r - 1) long_result /= (i + 1); } return (int) long_result; }};

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.

實(shí)現(xiàn)代碼

class Solution {public: int uniquePathsWithObstacles(vector<vector<int>>& g) { int row = g.size(); int column = row > 0?g[0].size():0; int v[101][101] = {0}; v[1][1] = 1 - g[0][0]; for(int i = 1; i <= row; i++) { for(int j = 1; j <= column; j++) { if((i != 1 || j != 1) && g[i-1][j-1]) v[i][j] = 0; else v[i][j] += v[i-1][j] + v[i][j-1]; } } return v[row][column]; }};
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 安国市| 远安县| 惠水县| 秦皇岛市| 中山市| 清水县| 界首市| 定西市| 当涂县| 攀枝花市| 二连浩特市| 勐海县| 北海市| 莱西市| 余干县| 高密市| 唐山市| 宾阳县| 潮安县| 肃北| 文登市| 土默特右旗| 乌海市| 宁远县| 蓬溪县| 青神县| 大厂| 海林市| 邵阳市| 仁布县| 山西省| 永寿县| 名山县| 房山区| 桓仁| 黄梅县| 康乐县| 靖州| 巴林右旗| 仙游县| 淮阳县|