lintcode 反转ASCII编码字符串

lintcode 反转ASCII编码字符串

描述

给定一个由ascii编码的字符串(例如,“ABC”可以编码为“656667”),您需要编写一个将编码字符串作为输入并返回反转的解码字符串的函数。

您可以假设答案字符串中只有大写字母。

样例

样例1

输入: “7976766972”
输出: “HELLO”
样例2

输入: “656667”
输出: “CBA”

思考

不断对输入的字符串截取长度为2的字符串,,转为int,再转为char,从后往前构造新的字符串

代码

class Solution {
public:
    /**
     * @param encodeString: an encode string
     * @return: a reversed decoded string
     */
    string reverseAsciiEncodedString(string &encodeString) {
        // Write your code here
        string res;
        res.resize(encodeString.length() / 2);
        int j = 0;
        for (int i = res.length() - 1; i >= 0; i--) {
            int temp = atoi(encodeString.substr(j, 2).c_str());
            char c = temp;
            res[i] = c;
            j += 2;
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_40147449/article/details/88842416