Leetcode - 12整数转罗马数字

12 整数转罗马数字

题目描述

Leetcode - 12整数转罗马数字_第1张图片
Leetcode - 12整数转罗马数字_第2张图片

题目分析

按照“一一对应”原则,注意对900,400,90,40,9,4特殊处理即可。

代码如下:

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

Leetcode - 12整数转罗马数字_第3张图片

你可能感兴趣的:(Leetcode)