Decode Ways - LeetCode

本文探讨了一道关于字符串解码方式数量的问题,并通过动态规划的方法给出了清晰的解答。通过对不同字符组合的分析,文章详细解释了如何计算可能的解码路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


开始写了一个标准的DP,但是由于对于题意中的0没有弄清楚,所以没过:

public class Solution {
public int numDecodings(String s) {
		if(s == null)
			return 0;
		else if(s.length() == 0)
			return 0;
        int[] method = new int[s.length()];
        for(int i = 0; i < s.length(); i ++){
        	char c = s.charAt(i);
        	if(!isc(c))
        	return 0;
        	if(i == 0){
        		if(isc(c))
        			method[i] = 1;
        		else 
        			method[i] = 0;
        	}
        	else if(i == 1){
        		int one = s.charAt(0) - '0';
        		int two = s.charAt(1) - '0';
        		if(!isc(c)){
        			method[1] = method[0];
        		}
        		else 
        			{
        			if(isc(s.charAt(0))){
        				if(one <= 2 && two <= 6)
        					method[1] = 2;
        				else
        					method[1] = 1;
        				}
        			else{
        				method[1] = 1;
        			}
        			}
        	}
        	else{
        		int one = s.charAt(i - 2) - '0';
        		int two = s.charAt(i - 1) - '0';
        		if(one <= 2 && two <= 6){
        			method[i] = method[i - 1] + method[i - 2];
        		}
        		else{
        			method[i] = method[i - 1] ;
        		}
        	}
        }
		return method[s.length() - 1];
    }
public boolean isc(char c){
	 if(c - '0' <= 0 || c - '0' > 26)
		 return false;
	 else return true;
}
}

找到一个其他博主的解法,很清晰:

定义d 为0~9中任意一个。

例如:"137" -> ddd,或者也可以写为 "137" -> "1d7"。

定义F[i]表示S的子串S[0..i]的decode ways。

假设我们已经知道了F[0]~F[i],现在需要求解F[i+1]。

先不考虑边界,就考虑一般情况:

1.S[i+1] == '0',如果S[i]为'1'或者'2',F[i+1] = F[i],否则无解;

2.如果S[i]为'1',F[i+1] = F[i] + F[i-1](例如"xxxxxx118",可以是"xxxxxx11" + "8",也可以是"xxxxxx1" + "18");

3.如果S[i]为'2',当S[i+1] <= '6'时,F[i+1] = F[i] + F[i-1] (最大的Z为"26","27""28""29"不存在),当S[i+1] > '6'时,F[i+1] = F[i] (例如"xxxxxx28",只能是"xxxxxx2" + "8")。


class Solution {
public:
    int numDecodings(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (s.empty()) {
            return 0;
        }
        int length = s.length();   
        int f[length];
        memset(f, 0, sizeof(int) * length);
        for (int i = 0; i < length; i++) {
            if (s[i] < '0' || s[i] > '9') {
                return 0;
            }
            else if (s[i] == '0') {
                if (i == 0 || s[i - 1] == '0' || s[i - 1] > '2') {
                    return 0;
                }
                else {
                    f[i] = i > 1 ? f[i - 2] : 1;
                }
            }
            else {
                if (i > 0 && (s[i - 1] == '1' || s[i - 1] == '2' && s[i] <= '6')) {
                    f[i] = (i > 0 ? f[i - 1] : 1) + (i > 1 ? f[i - 2] : 1);
                }    
                else {
                    f[i] = i > 0 ? f[i - 1] : 1;
                }
            }
        }
        return f[length - 1];
    }
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值