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

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];

    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值