Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created December 24, 2018 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/18cf6a63c49fb6ce1ebe78fa8045b8d7 to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/18cf6a63c49fb6ce1ebe78fa8045b8d7 to your computer and use it in GitHub Desktop.
class Solution {
public:
int uniquePaths(int m, int n) {
if (m > n) return uniquePaths(n, m);
vector<int> pre(m, 1);
vector<int> cur(m, 1);
for (int j = 1; j < n; j++) {
for (int i = 1; i < m; i++)
cur[i] = cur[i - 1] + pre[i];
swap(pre, cur);
}
return pre[m - 1];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment