LeetCode--115. 不同的子序列(java)

原题链接
在这里插入图片描述

	public int numDistinct(String s, String t) {
		int[][] dp = new int[t.length() + 1][s.length() + 1];
		for(int i = 0;i < dp[0].length;i++) {
			//dp[0]就是 t 的前 0 个字符,即空串
			dp[0][i] = 1;//s 的前 i 个字符构成的子串,最多包含一个空串
		}
		for(int i = 1;i < dp.length;i++) {
			for(int j = 1;j < dp[0].length;j++) {
				if(t.charAt(i - 1) == s.charAt(j - 1)) {
					dp[i][j] = dp[i][j - 1] + dp[i - 1][j - 1];
				}else {
					dp[i][j] = dp[i][j - 1];
				}
			}
		}
		return dp[dp.length - 1][dp[0].length - 1];
    }
发布了48 篇原创文章 · 获赞 4 · 访问量 2945

猜你喜欢

转载自blog.csdn.net/QinLaoDeMaChu/article/details/104069393