Decode way


  • 0
    E

    “”“
    public static int numDecodings(String s) {
    char[] chars=s.toCharArray();
    int[] count=new int[chars.length];
    if (s.isEmpty()){
    return 0;
    }
    if ((chars[0]-'0')==0){
    return 0;
    }else {
    count[0]=1;
    }
    for (int i=1;i<chars.length;i++){
    if (((10*(chars[i-1]-'0')+(chars[i]-'0'))==0)){
    return 0;
    }
    if ((chars[i]-'0')==0 && (chars[i-1]-'0')>2){
    return 0;
    }
    if ((chars[i]-'0')==0){
    if (i>=2){
    count[i]=count[i-2];
    }else {
    count[i]=count[i-1];
    }
    continue;
    }
    if (((10*(chars[i-1]-'0')+(chars[i]-'0'))>26) || (chars[i-1]-'0')==0){
    count[i]=count[i-1];
    continue;
    }
    if (i>=2){
    count[i]=count[i-1]+count[i-2];
    }else {
    count[i]=count[0]+1;
    }

        }
        return count[chars.length-1];
    }
    

    ”“”


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.