392.判断子序列
如果用dp判断true or false无法满足,所以dp用来表示以下标i-1为结尾的字符串s,和以下标j-1为结尾的字符串t,相同子序列的长度
func isSubsequence(s string, t string) bool {dp := make([][]int, len(s) + 1)for i := 0; i < len(dp); i++{dp[i] = make([]int, len(t) + 1)} for i := 1; i <= len(s); i++{for j := 1; j <= len(t); j++{if s[i - 1] == t[j - 1]{dp[i][j] = dp[i - 1][j - 1] + 1}else{//dp[i][j] = dp[i - 1][j]dp[i][j] = dp[i][j - 1]}}}if dp[len(s)][len(t)] == len(s){return true}return false
}
115.不同的子序列
dp:= make([][]int,len(s)+1)for i:=0;i<len(dp);i++{dp[i] = make([]int,len(t)+1)}// 初始化for i:=0;i<len(dp);i++{dp[i][0] = 1}// dp[0][j] 为 0,默认值,因此不需要初始化for i:=1;i<len(dp);i++{for j:=1;j<len(dp[i]);j++{if s[i-1] == t[j-1]{dp[i][j] = dp[i-1][j-1] + dp[i-1][j]}else{dp[i][j] = dp[i-1][j]}}}return dp[len(dp)-1][len(dp[0])-1]