From 31e6a1ad95de2a4652796d4367b84655d3653008 Mon Sep 17 00:00:00 2001 From: Guanzhong Pan Date: Wed, 25 Jan 2023 20:10:53 +0000 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A00062.=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=B7=AF=E5=BE=84.md=20C=E8=AF=AD=E8=A8=80=E6=BB=9A=E5=8A=A8?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0062.不同路径.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0062.不同路径.md b/problems/0062.不同路径.md index 2367587c..4c0e2279 100644 --- a/problems/0062.不同路径.md +++ b/problems/0062.不同路径.md @@ -436,6 +436,26 @@ int uniquePaths(int m, int n){ } ``` +滚动数组解法: +```c +int uniquePaths(int m, int n){ + int i, j; + + // 初始化dp数组 + int *dp = (int*)malloc(sizeof(int) * n); + for (i = 0; i < n; ++i) + dp[i] = 1; + + for (j = 1; j < m; ++j) { + for (i = 1; i < n; ++i) { + // dp[i]为二维数组解法中dp[i-1][j]。dp[i-1]为二维数组解法中dp[i][j-1] + dp[i] += dp[i - 1]; + } + } + return dp[n - 1]; +} +``` + ### Scala ```scala