-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path62.unique-paths.cpp
50 lines (42 loc) · 945 Bytes
/
62.unique-paths.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* @lc app=leetcode id=62 lang=cpp
*
* [62] Unique Paths
*/
// @lc code=start
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <stack>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
// #define DEBUG
class Solution {
public:
int uniquePaths(int m, int n) {
int dp[m][n];
memset(dp, 0, sizeof(int) * m * n);
dp[0][0] = 1;
for (int y = 1; y < m; y++) {
dp[y][0] = 1;
}
for (int x = 1; x < n; x++) {
dp[0][x] = 1;
}
for (int y = 1; y < m; y++) {
for (int x = 1; x < n; x++) {
dp[y][x] = dp[y][x-1] + dp[y-1][x];
#ifdef DEBUG
cout << dp[y][x] << ", ";
#endif
}
#ifdef DEBUG
cout << "\n";
#endif
}
return dp[m - 1][n - 1];
}
};
// @lc code=end