leetcode 12 Integer to Roman

2018-06-04 11:53:43 浏览数 (1)

代码语言:javascript复制
class Solution {
public:
    string intToRoman(int num) {
        string res = "";
        vector<int> val{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        vector<string> str{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        for (int i = 0; i < val.size();   i) {
            while (num >= val[i]) {
                num -= val[i];
                res  = str[i];
            }
        }
        return res;
    }
};

0 人点赞