LeetCode 题解(15): Decode Ways

题目:

A message containing letters from A-Z is being encoded to numbers using the following mapping:

'A' -> 1
'B' -> 2
...
'Z' -> 26

Given an encoded message containing digits, determine the total number of ways to decode it.

For example,
Given encoded message "12",it could be decoded as "AB" (1 2) or"L" (12).

The number of ways decoding "12" is 2.


题解:

动态规划,写的十分之丑。

class Solution {
public:
    int numDecodings(string s){
        if(!s.length() || s[0] == '0')
            return 0;
        int* count = new int[s.length()];
        count[0] = 1;
        for(int i = 1; i < s.length(); i++)
        {
            if(i < s.length() - 1 && s[i] == '0' && s[i+1] == '0') //连续两个00的情况,无意义,直接返回0
                return 0;
            else if(s[i] == '0' && s[i-1] > '2')//30,40,50...90的情况,无意义,直接返回0
                return 0;
            else if(s[i] == '0' && i - 2 >= 0)//s = '110'
                count[i] = count[i-2];
            else if(s[i] == '0' )
                count[i] = count[i-1];
            else if(i - 2 >= 0 && s[i-1] != '0' && strToInt(s.substr(i-1,2)) <= 26)
                count[i] = count[i-1] + count[i-2];
            else if(s[i-1] != '0' && strToInt(s.substr(i-1,2)) <= 26)
                count[i] = count[i-1] + 1;
	    else
	        count[i] = count[i-1];
        }
        return count[s.length()-1];
    }
    
    int static strToInt(string s)
    {
        assert(s.length() == 2);
        int result = (s[0]-'0') * 10 + s[1]-'0';
        return result;
    }
};

若干个月后重做,写的愈发丑。

class Solution {
public:
    int numDecodings(string s) {
        if(s.length() == 0 || s[0] == '0')
            return 0;
        if(s.length() == 1)
            return 1;
        int* ways = new int[s.length()];
        ways[0] = 1;
        if(stoi(s.substr(0,2)) == 10 || stoi(s.substr(0,2)) == 20) ways[1] = 1;
        else if(stoi(s.substr(0,2)) >= 11 && stoi(s.substr(0,2)) <= 26)
            ways[1] = 2;
        else if(s[1] == '0' && s[0] - '3' >= 0)
            return 0;
        else
            ways[1] = 1;

        for(int i = 2; i < s.length(); i++) {
            if(s[i] == '0' && (s[i-1] == '0' || s[i-1] - '3' >= 0))
                return 0;
            else if(s[i] == '0')
                ways[i] = ways[i-2];
            else if(stoi(s.substr(i-1,2)) <= 9)
                ways[i] = ways[i-1];
            else if(stoi(s.substr(i-1,2)) <= 26)
                ways[i] = ways[i-2]+ways[i-1];
            else ways[i] = ways[i-1];
        }
        
        return ways[s.length()-1];
    }
    
};

Java版:写的更是丑。

public class Solution {
    public int numDecodings(String s) {
        if(s.length() == 0)
            return 0;
        if(s.length() == 1 && s.charAt(0) != '0')
            return 1;
            
        int[] ways = new int[s.length()];
        if(s.charAt(0) == '0')
            return 0;
        else
            ways[0] = 1;
            
        if(s.length() > 2)
            if((Integer.parseInt(s.substring(0,2)) >= 11 && Integer.parseInt(s.substring(0,2)) <= 19) || (Integer.parseInt(s.substring(0,2)) >= 21 && Integer.parseInt(s.substring(0,2)) <= 26))
                ways[1] = 2;
            else if(s.charAt(1) == '0' && s.charAt(0) - '3' >= 0)
                return 0;
            else
                ways[1] = 1; 
        else if(s.length() == 2)
            if((Integer.parseInt(s.substring(0)) >= 11 && Integer.parseInt(s.substring(0)) <= 19) || (Integer.parseInt(s.substring(0)) >= 21 && Integer.parseInt(s.substring(0)) <= 26))
                ways[1] = 2;
            else if(s.charAt(1) == '0' && s.charAt(0) - '3' >= 0)
                return 0;
            else
                ways[1] = 1;        
            
        for(int i = 2; i < s.length(); i++) {
            if((s.charAt(i-1) == '0' && s.charAt(i) == '0') || (s.charAt(i) == '0' && s.charAt(i-1) - '3' >= 0))
                return 0;
            if(s.charAt(i-1) == '0' || (s.charAt(i) == '0' && s.charAt(i-1) - '2' <= 0))
                ways[i] = ways[i-2];
            else if((Integer.parseInt(s.substring(i-1, i+1)) >= 11 && Integer.parseInt(s.substring(i-1, i+1)) <= 19) || (Integer.parseInt(s.substring(i-1, i+1)) >= 21 && Integer.parseInt(s.substring(i-1, i+1)) <= 26))
                ways[i] = ways[i-1] + ways[i-2];
            else
                ways[i] = ways[i-1];
        }
        return ways[s.length()-1];
    }
}

Python版:

class Solution:
    # @param s, a string
    # @return an integer
    def numDecodings(self, s):
        if len(s) == 0:
            return 0
        if len(s) == 1 and s[0] != '0':
            return 1
        
        ways = ['0'] * len(s)
        
        if s[0] == '0':
            return 0
        else:
            ways[0] = 1
            
        if (int(s[0]+s[1]) >= 11 and int(s[0]+s[1]) <= 19) or (int(s[0]+s[1]) >= 21 and int(s[0]+s[1]) <= 26):
            ways[1] = 2
        elif int(s[0]) >= 3 and s[1] == '0':
            return 0
        else:
            ways[1] = 1
            
        for i in range(2, len(s)):
            if (s[i-1] == '0' and s[i] == '0') or (int(s[i-1]) >= 3 and s[i] == '0'):
                return 0
            elif s[i] == '0':
                ways[i] = ways[i-2]
            elif (int(s[i-1]+s[i]) >= 11 and int(s[i-1]+s[i]) <= 19) or (int(s[i-1]+s[i]) >= 21 and int(s[i-1]+s[i]) <= 26):
                ways[i] = ways[i-2] + ways[i-1]
            else:
                ways[i] = ways[i-1]
                
        return ways[len(s)-1]


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值