[LeetCode] 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.

 

Hide Tags
 Dynamic Programming String
 
思路1:递归,也就是dfs,功能正确,但大数据时超时
 
class Solution {
    int m_cnt;
    public:
        void numDecode(int dep, string s)
        {
            if(dep == s.size())
            {
                m_cnt ++;
                return;
            }

            if( s[dep] >= '1' && s[dep] <= '9') 
                numDecode(dep+1, s);
            else if( dep >= 1 && s[dep] == '0' &&
                    (s[dep-1] == '1' || s[dep-1] == 2) )
                numDecode(dep+1, s);

            if((dep+1) < s.size())
            {
                if( s[dep] == '1' || (s[dep] == '2' &&
                s[dep+1] >= '0' && s[dep+1] <= '6') )
                    numDecode(dep+2, s);
            }
        }
        int numDecodings(string s) {
            m_cnt = 0;
            numDecode(0, s);
            return m_cnt;
        }
};

 



思路二:dp

class Solution {
    public:
        int numDecodings(string s) {

            if(s.size() == 0)
                return 0;

            int f[s.size()];
            if(s[0] >= '1' && s[0] <= '9')
                f[0] = 1;
            else
                f[0] = 0;

            if(s.size() == 1)
                return f[0];

            if(s[1] == '0')
            {
                if(s[0] == '1' || s[0] == '2')
                    f[1] = 1;
                else
                    return 0;
            }
            else
            {
                if( s[0] == '1' || ( s[0] == '2' &&
                        s[1] >= '1' && s[1] <= '6' ))
                    f[1] = 2;
                else// default: if(s[i] >= '1' && s[i] <= '9' )
                    f[1] = f[0];
            }


            for(int i = 2; i < s.size(); i ++)
            {
                if(s[i] == '0')
                {
                    if(s[i-1] == '1' || s[i-1] == '2')
                        f[i] = f[i-2];
                    else
                        return 0;
                }
                else
                {
                    if( s[i-1] == '1' || (s[i-1] == '2' &&
                            s[i] >= '1' && s[i] <= '6') )
                        f[i] = f[i-1] + f[i-2];
                    else// default: if(s[i] >= '1' && s[i] <= '9' )
                        f[i] = f[i-1];
                }
            }

            return f[s.size() -1];
        }
};

 

思路三:由于f[i] 只和f[i-1] f[i-2] 有关系,可用tmp1,tmp2 保存f[i-1] f[i-2] 使得空间复杂度变成O(1)

 

 

 



posted @ 2015-03-30 17:52  穆穆兔兔  阅读(182)  评论(0编辑  收藏  举报