# 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`.

## AC代码

``````#include <iostream>
#include <cstdio>
#include <string>
#include <vector>

using namespace std;

int numDecodings(string s)
{
if(s == "")
return 0;
vector<int> f;
f.resize(s.length());
int i = 0;
for(; i < s.length(); i++)
{
f[i] = 0;
if(i >= 1)
{
string tmp(s, i-1, 2);
if("10" <= tmp && tmp <= "26")
{
if(i > 1)
f[i] += f[i - 2];
else
f[i] += 1;
}

if('1' <= s[i] && s[i] <= '9')
f[i] += f[i-1];
}
else
{
if('1' <= s[i] && s[i] <= '9')
f[i] = 1;
}
}
return f[i-1];
}

int main()
{
string s;

#ifndef ok
freopen("in1.in", "r", stdin);
freopen("std1.out","w",stdout);
#endif

while(cin>>s)
{
cout<<numDecodings(s)<<endl;
}

#ifndef ok
fclose(stdin);
fclose(stdout);
#endif

return 0;
}
``````