62. Unique Paths 类别:动态规划 难度:medium
2017-01-08 22:11
597 查看
题目:
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?
Above is a 3 x 7 grid. How many possible unique paths are there?
思路:
对于格点(i,j)。由于只能从上格点(i-1,j)或左格点(i,j-1)到达,并且两者路径是不重复的,因此path[i][j] = path[i-1][j]+path[i][j-1]。
程序:
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int> > res(m + 1, vector<int>(n + 1, 1));
for(int i = 2;i <= m;i++)
{
for(int j = 2;j <= n;j++)
res[i][j] = res[i - 1][j] + res[i][j - 1];
}
return res[m]
;
}
};
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?
Above is a 3 x 7 grid. How many possible unique paths are there?
思路:
对于格点(i,j)。由于只能从上格点(i-1,j)或左格点(i,j-1)到达,并且两者路径是不重复的,因此path[i][j] = path[i-1][j]+path[i][j-1]。
程序:
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int> > res(m + 1, vector<int>(n + 1, 1));
for(int i = 2;i <= m;i++)
{
for(int j = 2;j <= n;j++)
res[i][j] = res[i - 1][j] + res[i][j - 1];
}
return res[m]
;
}
};
相关文章推荐
- 376. Wiggle Subsequence 类别:动态规划 难度:medium
- 357. Count Numbers with Unique Digits 类别:动态规划 难度:medium
- 343. Integer Break 类别:动态规划 难度:medium
- 300. Longest Increasing Subsequence 类别:动态规划 难度:medium
- 120. Triangle 类别:动态规划 难度:medium
- 413. Arithmetic Slices 类别:动态规划 难度:Medium
- 63. Unique Paths II 类别:动态规划 难度:medium
- 64. Minimum Path Sum 类别:动态规划 难度:medium
- 96. Unique Binary Search Trees 类别:动态规划 难度:medium
- 309. Best Time to Buy and Sell Stock with Cooldown 类别:动态规划 难度:medium
- 121. Best Time to Buy and Sell Stock 类别:动态规划 难度:easy
- 70. Climbing Stairs 类别:动态规划 难度:easy
- 198. House Robber 类别:动态规划 难度:easy
- 55. Jump Game 类别:贪心算法 难度:medium
- 134. Gas Station 类别:贪心算法 难度:medium
- 215. Kth Largest Element in an Array 难度:medium 类别:分治
- 207. Course Schedule 难度:medium 类别:图
- 241. Different Ways to Add Parentheses 难度:medium 类别:分治
- 210. Course Schedule II 难度:medium 类别:图
- 53. Maximum Subarray 难度:medium 类别:分治