91. Decode Ways
# Medium
Dynamic Programming
dp[i]
表示前i个元素有多少种拆分方法
看起来简单,但很不容易写对,要注意以下四点
"1000"当前数字为零,返回0
"127" 因为27>26,不能组合在一起
"120" 其中20必须组合在一起
“130” 其中30不能组合在一起,则拆分成3和0,回归到情况1
必须要跟上面的代码写得一模一样,不然就会出错,DP能够处理更大的数据。由于dp[i]
最多只取决于dp[i-1]
和 dp[i-2]
,时间复杂度为 ,由于只使用了dp这个 数组,所以空间复杂度为
Last updated