LeetCode || Integer to Roman

Integer to Roman

  Total Accepted: 6855  Total Submissions: 21222 My Submissions

Given an integer, convert it to a roman numeral.

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



我的初始代码就是各种case,其中要注意的就是 900、400、90、40、9、4,这些比较特殊:
class Solution {
void process(string &res, int k, int times){
    if(times){
        int i;
        switch(k){
            case 1000:
                for(i=0; i<times; ++i)
                    res.append("M");
                break;
            case 500:
                for(i=0; i<times; ++i)
                    res.append("D");
                break;
            case 100:
                if(times==4)
                    res.append("CD");
                else if(times==9)
                    res.append("CM");
                else{
                    for(i=0; i<times; ++i)
                        res.append("C");
                }
                break;
            case 50:
                for(i=0; i<times; ++i)
                    res.append("L");
                break;
            case 10:
                if(times==4)
                    res.append("XL");
                else if(times==9)
                    res.append("XC");
                else{
                    for(i=0; i<times; ++i)
                        res.append("X");
                }
                break;
            case 1:
                switch(times){
                case 9:
                    res.append("IX");
                    break;
                case 8:
                    res.append("VIII");
                    break;
                case 7:
                    res.append("VII");
                    break;
                case 6:
                    res.append("VI");
                    break;
                case 5:
                    res.append("V");
                    break;
                case 4:
                    res.append("IV");
                    break;
                case 3:
                    res.append("III");
                    break;
                case 2:
                    res.append("II");
                    break;
                case 1:
                    res.append("I");
                    break;
                }
                break;
            default:
                break;
        }
    }
    //cout<<res<<endl;
}
public:
    string intToRoman(int num) {
        string res;
        int temp=num, times;
        res.clear();
        if(num!=0){
            times=temp/1000;
            process(res, 1000, times);
            temp=temp%1000;

            if(temp/100==9){
                process(res, 100, 9);
                temp=temp%100;
            }
            else{
                times=temp/500;
                process(res, 500, times);
                temp=temp%500;
            }

            times=temp/100;
            process(res, 100, times);
            temp=temp%100;

            if(temp/10==9){
                process(res, 10, 9);
                temp=temp%10;
            }
            else{
                times=temp/50;
                process(res, 50, times);
                temp=temp%50;
            }
            times=temp/10;
            process(res, 10, times);
            temp=temp%10;

            times=temp/1;
            process(res, 1, times);

        }
        //cout<<res<<endl;
        return res;
    }
};



AC之后又去看了下论坛,果然发现了精简的代码,学习后自己写了一下:
class Solution {
public:
    string intToRoman(int num) {
        string res;
        res.clear();
        map<int, string> mp;
        mp[1000]= "M";
        mp[900]= "CM";
        mp[500]= "D";
        mp[400]= "CD";
        mp[100]= "C";
        mp[90]= "XC";
        mp[50]= "L";
        mp[40]= "XL";
        mp[10]= "X";
        mp[9]= "IX";
        mp[5]= "V";
        mp[4]= "IV";
        mp[1]= "I";

        for(map<int, string>::reverse_iterator it=mp.rbegin(); num!=0&&it!=mp.rend(); it++){
            if(num>=it->first){
                return res.append(it->second)+intToRoman(num-it->first);
            }
        }
        return res;
    }
};





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值