91. Decode Ways 数字转字母的不同编码方式

来源:互联网 发布:网络抓包软件debugger 编辑:程序博客网 时间:2024/06/13 23:27

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.


1. 看别人的 (这个我不会)

class Solution {public:    bool isValid(char c){        return c <= '9' && c >'0';    }    bool isvalid(char c1, char c2){        if(c1 == '1')            return c2 <='9' && c2 >='0';        if(c1 == '2')            return c2 <='6' && c2 >='0';        return false;    }    int numDecodings(string s) {        int len = s.size();        if(len <= 0)        return 0;        vector<int>res(len+1,0);        res[0] = 1;        for(int i = 0; i < len; i++){            if(isValid(s[i]))                res[i+1] += res[i];            if(isvalid(s[i-1],s[i]))                res[i+1] += res[i-1];        }        return res[len];    }};

0 0
原创粉丝点击