diff options
| author | Steve Lee <me@xiangyangli.com> | 2017-12-15 10:14:03 +0800 |
|---|---|---|
| committer | Steve Lee <me@xiangyangli.com> | 2017-12-15 10:14:03 +0800 |
| commit | 2e0e0f39d49296f0ffb99aea533a527174521d61 (patch) | |
| tree | 03c45deb8e1ed80807d7f211361d346644e95a78 /Computer_Science/leetcode/62-unique_paths.c | |
| parent | 25b4f2abd597ffba22cccea8587d00540577197d (diff) | |
| download | 42-2e0e0f39d49296f0ffb99aea533a527174521d61.tar.xz 42-2e0e0f39d49296f0ffb99aea533a527174521d61.zip | |
62
Diffstat (limited to 'Computer_Science/leetcode/62-unique_paths.c')
| -rw-r--r-- | Computer_Science/leetcode/62-unique_paths.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Computer_Science/leetcode/62-unique_paths.c b/Computer_Science/leetcode/62-unique_paths.c new file mode 100644 index 0000000..6f613fa --- /dev/null +++ b/Computer_Science/leetcode/62-unique_paths.c @@ -0,0 +1,33 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int uniquePaths(int m, int n) +{ + int i, j; + int *matrix = malloc(sizeof(int) * m * n); + + if(m <= 1 || n <= 1) + return 1; + + for(i = 0; i < m; i++) { + for(j = 0; j < n; j++) { + *(matrix + i * n + j) = 1; + } + } + + for(i = 1; i < m; i++) { + for(j = 1; j < n; j++) { + *(matrix + i * n + j) = + *(matrix + i * n + j-1) + + *(matrix + (i - 1) * n + j); + } + } + + return *(matrix + m * n - 1); +} + +int main() +{ + printf("%d\n", uniquePaths(3, 7)); +} |
