我是靠谱客的博主 欢喜胡萝卜,这篇文章主要介绍leetcode 日经贴,Cpp code -Integer to Roman,现在分享给大家,希望可以做个参考。

Integer to Roman

class Solution {
public:
    string getstring(string base, int v) {
        string ret = "";
        //v in [0, 9]
        if (v <= 3) {
            for (int i = 0; i < v; ++i) {
                ret += base[0];
            }
        } else if (v == 4) {
            ret = base.substr(0, 2);
        } else if (v < 9) {
            ret = base[1];
            for (int i = 0; i < v - 5; ++i) {
                ret += base[0];
            }
        } else if (v == 9) {
            ret = base[0];
            ret += base[2];
        }
        return ret;
    }
    string intToRoman(int num) {
        string ans = "";
        string bases[3] = {"IVX", "XLC", "CDM"};
        int th = num / 1000;
        num %= 1000;
        for (int i = 0; i < th; ++i) {
            ans += 'M';
        }
        ans += getstring(bases[2], num / 100);
        num %= 100;
        ans += getstring(bases[1], num / 10);
        ans += getstring(bases[0], num % 10);
        return ans;
    }
};


最后

以上就是欢喜胡萝卜最近收集整理的关于leetcode 日经贴,Cpp code -Integer to Roman的全部内容,更多相关leetcode内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(93)

评论列表共有 0 条评论

立即
投稿
返回
顶部