[leetcode] 62. Unique Paths
2016-01-26 15:36
417 查看
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?
![](http://leetcode.com/wp-content/uploads/2014/12/robot_maze.png)
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
这道题是机器人想从左上角走到右下角,问可能的路径数,题目难度为Medium。
在任一位置,机器人有两个选择,向下或向右,把向下和向右两个位置所有可能的路径相加即得到了此位置到右下角所有可能的路径数。一道比较典型的动态规划题目,具体代码:
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?
![](http://leetcode.com/wp-content/uploads/2014/12/robot_maze.png)
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
这道题是机器人想从左上角走到右下角,问可能的路径数,题目难度为Medium。
在任一位置,机器人有两个选择,向下或向右,把向下和向右两个位置所有可能的路径相加即得到了此位置到右下角所有可能的路径数。一道比较典型的动态规划题目,具体代码:
class Solution { public: int uniquePaths(int m, int n) { if(m < 1 && n < 1) return 0; vector<vector<int>> rst(m, vector<int>(n, 0)); for(int i=0; i<m; i++) rst[i][n-1] = 1; for(int i=0; i<n; i++) rst[m-1][i] = 1; for(int i=m-2; i>=0; i--) { for(int j=n-2; j>=0; j--) { rst[i][j] = rst[i+1][j] + rst[i][j+1]; } } return rst[0][0]; } };
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解