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步爬两步到达,因此
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)种不同的走法,可以从右下角的左边一个格子向右走一步到达,也可以从上面一个格子向下走一步到达(如果这些格子存在的话),即
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]; }};
- leetcode第十二周解题总结--动态规划
- leetcode第十三周解题总结--动态规划(二)
- leetcode动态规划总结
- 第十二周 动态规划(二)
- Leetcode解题笔记 53. Maximum Subarray [Easy] 动态规划
- Leetcode解题笔记 64. Minimum Path Sum [Medium] 动态规划
- Leetcode解题笔记 70. Climbing Stairs [Easy] 动态规划
- Leetcode解题笔记 62. Unique Paths [Medium] 动态规划
- Leetcode解题笔记 63. Unique Paths II [Medium] 动态规划
- 动态规划解题
- 动态规划解题法
- PAT1068 动态规划解题
- 动态规划解题入门
- LeetCode总结 -- 一维动态规划篇
- LeetCode总结,动态规划问题小结
- leetcode第七周解题总结
- leetcode第八周解题总结
- leetcode第十周解题总结
- 线程安全
- 工厂模式
- XDOJ 1024: 简单逆序对
- APP安全扫描网站
- mybatis逆向工程外键映射成相应的对象解决方法
- leetcode第十二周解题总结--动态规划
- Excel VBA高效办公应用-第六章-客户资料管理-Part1 (窗口冻结及排序)
- 2.3.7--Game of Connections--标准catalan
- 在Android Studio下使用Hierarchy Viewer
- Tensorflow C++ 学习(一) 搭建环境
- El表达式
- 动态规划
- python使用ftp
- 学习:java设计模式—工厂模式