leetcode 062 Unique Paths
2016-05-01 17:00
666 查看
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?
Note: m and n will be at most 100.
Subscribe to see which companies asked this question
// class Solution {
// public:
// int uniquePaths(int m, int n) {
// vector<vector<int> > matrix(100, vector<int>(100, 1));
// //matrix[i][j] = matrix[i][j-1]+matrix[i-1][j]
// for(int i = 1; i < n; i++) {
// for(int j = 1; j < m; j++) {
// matrix[i][j] = matrix[i][j-1]+matrix[i-1][j];
// }
// }
// return matrix[n-1][m-1];
// }
// }
class Solution {
public:
int uniquePaths(int m, int n) {
if(m > n) return uniquePaths(n, m);
vector<int> cur(m,1);
//matrix[i][j] = matrix[i][j-1]+matrix[i-1][j]
for(int j=1; j < n; j++) {
for(int i=1; i < m; i++) {
cur[i] += cur[i-1];
}
}
return cur[m-1];
}
}
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?
Note: m and n will be at most 100.
Subscribe to see which companies asked this question
// class Solution {
// public:
// int uniquePaths(int m, int n) {
// vector<vector<int> > matrix(100, vector<int>(100, 1));
// //matrix[i][j] = matrix[i][j-1]+matrix[i-1][j]
// for(int i = 1; i < n; i++) {
// for(int j = 1; j < m; j++) {
// matrix[i][j] = matrix[i][j-1]+matrix[i-1][j];
// }
// }
// return matrix[n-1][m-1];
// }
// }
class Solution {
public:
int uniquePaths(int m, int n) {
if(m > n) return uniquePaths(n, m);
vector<int> cur(m,1);
//matrix[i][j] = matrix[i][j-1]+matrix[i-1][j]
for(int j=1; j < n; j++) {
for(int i=1; i < m; i++) {
cur[i] += cur[i-1];
}
}
return cur[m-1];
}
}
相关文章推荐
- Codeforces 667C Reberland Linguistics (DP)
- Java源码分析--StringBuffer/StringBuilder
- 让requirejs不自动添加.js后缀
- Encapsulation and Requiring Files
- Encapsulation and Requiring Files
- 96. Unique Binary Search Trees
- SNMP++ 01-SNMP request time out问题的解决
- 300. Longest Increasing Subsequence
- 2016 UESTC Training for Data Structures R - Japan CDOJ 383 树状数组 逆序对
- 2016 UESTC Training for Data Structures Q - 昊昊爱运动 II CDOJ 1259 线段树+bitset
- 2016 UESTC Training for Data Structures P - 浑身难受 CDOJ 1276 树状数组
- 2016 UESTC Training for Data Structures O - 卿学姐种美丽的花 CDOJ 1344 线段树/树状数组
- 2016 UESTC Training for Data Structures N - 秋实大哥搞算数 CDOJ 1074 栈 表达式求值
- 2016 UESTC Training for Data Structures M - 卿学姐失恋了Ⅱ CDOJ 1350 汉诺塔 模拟
- 2016 UESTC Training for Data Structures L - 郭大侠与苦恼 CDOJ 1284 map+启发式合并
- link quiz
- UIImageVIew
- 2016 UESTC Training for Data Structures K - 郭大侠与甲铁城 CDOJ 1342 离线树状数组
- Analog linked list quiz
- 2016 UESTC Training for Data Structures J - 郭大侠与Rabi-Ribi CDOJ 1334 优先队列