leetcode第十二周解题总结--动态规划

来源:互联网 发布:ubuntu ssd hdd 编辑:程序博客网 时间:2024/06/05 17:59

70. Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positive integer.

题意解析:
爬梯子,需要爬n步到达顶端。但每次只能爬一步或者两步,问有多少种不同的爬法。

解题思路:
动态规划,假设爬x步有f(x)种不同的爬法,x步由x-1步爬一步到达,也可以由x-2步爬两步到达,因此

f(x)=f(x1)+f(x2)

class Solution {public:    int climbStairs(int n) {        if(n == 0 || n == 1) return 1;        vector<int> nums(n+1);         for(int i = 0; i <= n; i++) {            if(i == 1 || i == 0) {                nums[i] = 1;            } else {                nums[i] = nums[i-1] + nums[i-2];            }        }        return nums[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?

这里写图片描述

题意解析:
走格子,只能向右或者向下,问从m*n个格子的左上角走到右下角有多少种走法?

解题思路:
动态规划,假设m*n个格子从左上角走到右下角有f(m,n)种不同的走法,可以从右下角的左边一个格子向右走一步到达,也可以从上面一个格子向下走一步到达(如果这些格子存在的话),即

f(m,n)=f(m1,n)+f(m,n1)

class Solution {public:    int uniquePaths(int m, int n) {        if(m == 1 || n == 1) return 1;        vector<vector<int>> a;        for(int i = 0; i < m; i++){            vector<int> b(n);            a.push_back(b);            for(int j = 0; j < n; j++) {                if(i == 0 && j == 0){                    a[i][j] = 1;                    continue;                }                a[i][j] = 0;                if (i > 0){                    a[i][j] +=a[i - 1][j];                 }                if (j > 0){                    a[i][j] +=a[i][j - 1];                 }            }        }        return a[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.

题意解析:
和上题一样,但加入障碍物,用一个二维数组中的0或者1表示,1表示障碍。

解题思路:
思路和上题一样,只是多加一个障碍物的判断,如果这个位置被标注为障碍的话,该位置的走法数量为0。

class Solution {public:    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {        int m = obstacleGrid.size();        int n = obstacleGrid[0].size();        vector<vector<int>> a;        for(int i = 0; i < m; i++){            vector<int> b(n);            a.push_back(b);            for(int j = 0; j < n; j++) {                if (obstacleGrid[i][j] == 1) {                    a[i][j] = 0;                    continue;                }                if(i == 0 && j == 0){                    a[i][j] = 1;                    continue;                }                a[i][j] = 0;                if (i > 0 && obstacleGrid[i-1][j] == 0){                    a[i][j] +=a[i - 1][j];                 }                if (j > 0 && obstacleGrid[i][j-1] == 0){                    a[i][j] +=a[i][j - 1];                 }            }        }        return a[m - 1][n-1];    }};