릿코드(LEETCODE)

1309. Decrypt String from Alphabet to Integer Mapping

cepiloth 2020. 2. 16. 16:38
반응형

https://leetcode.com/problems/decrypt-string-from-alphabet-to-integer-mapping/

 

Source

class Solution {
public:
    
    string freqAlphabets(string s) {
        
        string ret;
        int size = s.size();
        int pos = 0;

        for (int i = 0; i < size; ) {
            unsigned char code[3];
            code[0] = s[i];
            code[1] = code[2] = -1;

            if (i + 1 < size)
                code[1] = s[i + 1];;

            if (i + 2 < size)
                code[2] = s[i + 2];

            if (code[2] == '#') {
                string d;
                d.push_back(code[0]);
                d.push_back(code[1]);
                ret += 'j' + atoi(d.c_str()) - 10;
                i += 3;
            }
            else {
                ret += 'a' + code[0] - '1';
                i++;
            }
        }

        return ret;
    }
};

 

반응형