麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

62. Unique Paths / 63. Unique Paths II

2019-11-10 19:17:40
字體:
供稿:網(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,從左上角到右下角有多少條路徑。

解法描述

法一:使用深度搜索。這種方法會超時(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ā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产黄色毛片 | h视频免费观看 | 男人天堂新地址 | 欧美a久久| 男人午夜视频 | 羞羞视频免费网站日本动漫 | 国产精品9191 | 国产一区二区精品在线观看 | 7777在线视频免费播放 | 欧美黄色视屏 | 特色一级黄色片 | 久久久精品视频免费看 | 中文字幕视频在线播放 | 露脸各种姿势啪啪的清纯美女 | 黄色毛片视频在线观看 | 真人一级毛片免费 | 夜间福利网站 | 色柚视频网站ww色 | 九草网| 国产精品剧情一区二区在线观看 | 九九精品视频免费 | 看毛片免费 | 黄污污网站 | 日韩色电影 | 免费国产wwwwwww网站 | 99麻豆久久久国产精品免费 | av国产片 | 92看片淫黄大片欧美看国产片 | 免费在线观看午夜视频 | 亚洲午夜在线观看 | 久久人人做 | 天天骑夜夜操 | 日本在线播放一区二区三区 | 久久精品成人 | 国产91免费看 | 久久久一区二区三区精品 | 国产精品久久久不卡 | 国产午夜精品久久久 | 久久久tv| 91精品国产91热久久久做人人 | 日本在线看片 |