0
点赞
收藏
分享

微信扫一扫

Leetcode 12. 整数转罗马数字(DAY 251)---- 后端面试题

爱读书的歌者 2022-02-26 阅读 38

文章目录


原题题目


在这里插入图片描述


代码实现(首刷自解)


class Solution {
public:
    string intToRoman(int num) {
        vector<pair<int,string>>   cnt{{1000,"M"},{900,"CM"},{500,"D"},{400,"CD"},
                                       {100,"C"},{90,"XC"},{50,"L"},{40,"XL"},
                                       {10,"X"},{9,"IX"},{5,"V"},{4,"IV"},{1,"I"}};

        string ret;
        int pos = 0,size = cnt.size();
        while(num)
        {
            for(int i = pos;i < size;++i)
            {
                const auto& pair = cnt[i];
                if(num < pair.first)
                {
                    ++pos;
                    continue;
                }
                num -= pair.first;
                ret += pair.second;
                break;
            }
        }  

        return ret;

    }
};
举报

相关推荐

0 条评论