13. Roman to Integer

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

S:

先判断是否由是由两个字母组成,若是则加入该值;若不是就加入单个字母的值

class Solution {
public:
    int romanToInt(string s) {
        unordered_map<string, int> map;
        initiatemap(map);
        int num = 0;
        for (int i = 0; i < s.size(); ++i) {
            if (i < s.size() - 1 && map.find(s.substr(i, 2)) != map.end()) {
                num += map[s.substr(i, 2)];
                i++;
            } else {
                num += map[s.substr(i, 1)];
            }
        }
        return num;
    }

    void initiatemap(unordered_map<string, int>& map) {
        map["M"] = 1000;
        map["CM"] = 900;
        map["D"] = 500;
        map["CD"] = 400;
        map["C"] = 100;
        map["XC"] = 90;
        map["L"] = 50;
        map["XL"] = 40;
        map["X"] = 10;
        map["IX"] = 9;
        map["V"] = 5;
        map["IV"] = 4;
        map["I"] = 1;
    }
};

class Solution {
public:
    unordered_map<char, int> roman;
    int romanToInt(string s) {
        initiate();
        int result = 0, size =s.size();
        for(int i = 0; i < size; ++i){
            string num = "";
            if((s[i] == 'I' && i < size-1 && (s[i+1] == 'V'||s[i+1] == 'X')) ||
               (s[i] == 'X' && i < size-1 && (s[i+1] == 'L'||s[i+1] == 'C')) ||
               (s[i] == 'C' && i < size-1 && (s[i+1] == 'D'||s[i+1] == 'M'))){
                    result += (roman[s[i+1]] - roman[s[i]]);
                    i++;
                }
            else result += roman[s[i]];
        }
        return result;
    }
    void initiate(){
        roman['I'] = 1; 
        roman['V'] = 5;
        roman['X'] = 10;
        roman['L'] = 50;
        roman['C'] = 100;
        roman['D'] = 500;
        roman['M'] = 1000;
    }
};

12. Integer to Roman

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

S:分别用两个数组存储数字和对应的罗马字母,根据运算规律循环得出结果

I : 1 V : 5 X : 10 L : 50 C : 100 D : 500 M : 1000
IV : 4 IX : 9 XL : 40 XC : 90 CD : 400 CM : 900
    string intToRoman(int num) {
        vector<int> vals = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        vector<string> syms = vector<string>{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", 
                                         "IX", "V", "IV", "I"};
        string result = "";
        for(int i = 0; i < vals.size(); ++i)
            for(int count = num/vals[i]; count > 0; --count) 
            {
                result += syms[i]; 
                num -= vals[i];
            }
        return result;
    }

273. Integer to English Words

Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231- 1.

For example,

123 -> "One Hundred Twenty Three"
12345 -> "Twelve Thousand Three Hundred Forty Five"
1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

S: divide num into chunks of words

注意Corner case 和末尾空格的处理

vector<string> thousands{"", "Thousand", "Million", "Billion"};
vector<string> twentyUp{"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",
                         "Eighty", "Ninety"};
vector<string> belowTwenty{"", "One", "Two", "Three", "Four","Five","Six","Seven",
                          "Eight","Nine","Ten", "Eleven","Twelve","Thirteen","Fourteen",
                          "Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
class Solution {
public:
    string numberToWords(int num) {
        if (num == 0) return "Zero";
        string result = "";
        for (int i = num, idx = 0; i > 0; i /= 1000, ++idx) {
            if (i % 1000) {
                result = helper(i % 1000) + thousands[idx] + " " +result;
            }
        }
        int zero = 0;
        int size = result.size();
        for (int i = size - 1; i >= 0 && result[i] == ' '; --i) {
            zero++;
        }
        return result.substr(0, size - zero);
    }
    string helper(int num) {
        string result = "";
        if (num / 100) {
            int i = num / 100;
            result += belowTwenty[i] + " " + "Hundred" + " ";
            num %= 100;
        }
        if (num >= 20) {
            result += twentyUp[num/10 - 2] + " ";
            num %= 10;
        }
        if (num > 0) result += belowTwenty[num] + " ";
        return result;
    }
};

results matching ""

    No results matching ""