aboutsummaryrefslogtreecommitdiff
path: root/Computer_Science/leetcode/62-unique_paths.c
diff options
context:
space:
mode:
Diffstat (limited to 'Computer_Science/leetcode/62-unique_paths.c')
-rw-r--r--Computer_Science/leetcode/62-unique_paths.c33
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));
+}