From 7fd07921a8d8470f9f597b7162b34e1b8c5d0b80 Mon Sep 17 00:00:00 2001 From: Yang Date: Mon, 14 Jun 2021 21:00:34 -0400 Subject: [PATCH] =?UTF-8?q?Update=200115.=E4=B8=8D=E5=90=8C=E7=9A=84?= =?UTF-8?q?=E5=AD=90=E5=BA=8F=E5=88=97.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add Java solution --- problems/0115.不同的子序列.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/problems/0115.不同的子序列.md b/problems/0115.不同的子序列.md index e54389aa..d3bc6d97 100644 --- a/problems/0115.不同的子序列.md +++ b/problems/0115.不同的子序列.md @@ -145,7 +145,28 @@ public: Java: - +```java +class Solution { + public int numDistinct(String s, String t) { + int[][] dp = new int[s.length() + 1][t.length() + 1]; + for (int i = 0; i < s.length() + 1; i++) { + dp[i][0] = 1; + } + + for (int i = 1; i < s.length() + 1; i++) { + for (int j = 1; j < t.length() + 1; j++) { + if (s.charAt(i - 1) == t.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]; + }else{ + dp[i][j] = dp[i - 1][j]; + } + } + } + + return dp[s.length()][t.length()]; + } +} +``` Python: ```python