From a35adcffbee7ff9c229cff38eff9506b396c0186 Mon Sep 17 00:00:00 2001 From: yqq Date: Mon, 27 Sep 2021 18:51:54 +0800 Subject: [PATCH] =?UTF-8?q?1035.=E4=B8=8D=E7=9B=B8=E4=BA=A4=E7=9A=84?= =?UTF-8?q?=E7=BA=BF.md,=20=E5=A2=9E=E5=8A=A0Golang=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1035.不相交的线.md | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/problems/1035.不相交的线.md b/problems/1035.不相交的线.md index a10fd381..8795a77d 100644 --- a/problems/1035.不相交的线.md +++ b/problems/1035.不相交的线.md @@ -109,6 +109,41 @@ class Solution: return dp[-1][-1] ``` + +Golang: + +```go + +func maxUncrossedLines(A []int, B []int) int { + m, n := len(A), len(B) + dp := make([][]int, m+1) + for i := range dp { + dp[i] = make([]int, n+1) + } + + for i := 1; i <= len(A); i++ { + for j := 1; j <= len(B); j++ { + if (A[i - 1] == B[j - 1]) { + dp[i][j] = dp[i - 1][j - 1] + 1; + } else { + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + } + } + } + return dp[m][n]; + +} + +func max(a, b int) int { + if a > b { + return a + } + return b +} +``` + + + JavaScript: ```javascript