LEETCODE - 【1271】十六进制魔术数字

class Solution {
public:
    string toHexspeak(string num) {
        stringstream ss;
        long long inter;
        //转16进制
        string hexret;
        ss << num;
        ss >> inter;
        ss.clear();
        ss << hex << inter;
        ss >> hexret;
        //获取转换关系
        map<char,char> transform = {
            {'a','A'},
            {'b','B'},
            {'c','C'},
            {'d','D'},
            {'e','E'},
            {'f','F'},
            {'1','I'},
            {'0','O'}
            };
        for(int i = 0; i < hexret.size(); ++i){
            if(transform.count(hexret[i]) == 0){
                return "ERROR";
            }
            hexret[i] = transform[hexret[i]];
        }
        return hexret;
    }
};

猜你喜欢

转载自www.cnblogs.com/wangqiwen-jer/p/12173726.html